【问题标题】:How can I loop this decision structure?我怎样才能循环这个决策结构?
【发布时间】:2020-02-10 21:16:18
【问题描述】:

对不起,我很难得到这个循环,但我会停止调整它并询问 SO。我希望决策结构循环,我希望它通过输入“4”来停止循环,并且我希望每次都打印“进行选择”提示。谢谢!

public class HW1Geo {
    public static void main(String[] args) {

        // This program was created in order to allow the user to calculate the areas of rectangles, triangles, and circles.

        System.out.println("Thank you for using the MCCH GeoCal program. \nWith this program, you will be able to find the area of three kinds of shapes.\nThis program uses doubles.");
        System.out.println();

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please make a selection:");
        System.out.println("1 - Area of a rectangle");
        System.out.println("2 - Area of a triangle");
        System.out.println("3 - Area of a circle");
        System.out.println("4 - End program");
        int select = keyboard.nextInt();

        // This decision structure with a nested loop will allow the user to continue to make selections until they decide to quite the program.

        while(select != 4) {

            if(select == 1) {
                System.out.println("Enter the length of the rectangle.");
                double length = keyboard.nextDouble();
                System.out.println("Enter the width of the rectangle");
                double width = keyboard.nextDouble();
                System.out.println("The area of the rectangle is: " + rectArea(length, width));
                break;
            }
            else if(select == 2) {
                System.out.println("Enter the height of the triangle.");
                double height = keyboard.nextDouble();
                System.out.println("Enter the size of the base of the triangle.");
                double base = keyboard.nextDouble();
                System.out.println("The area of the triangle is: " + triArea(height, base));
                break;
            }
            else if(select == 3) {
                System.out.println("Enter the radius of the circle.");
                double radius = keyboard.nextDouble();
                System.out.println("The area of the circle is: " + cirArea(radius));
                break;
            }
            else if(select != 1 && select != 2 && select != 3 && select != 4) {
                System.out.println("Incorrect input.");
                break;
            }

        }
        System.out.println("Thank you for using this program.");
    }

    // This method is used to find the area of a rectangle.
    public static double rectArea(double length, double width) {
        double area = length * width;
        return area;    
    }
    public static double triArea(double height, double base) {
        double area = 0.5 * base * height;
        return area;
    }
    public static double cirArea(double radius) {
        double area = Math.PI * Math.pow(radius, 2);
        return area;
    }
}

【问题讨论】:

  • 只需将提示(菜单)代码行和select = keyboard.nextInt();移入 while 循环顶部第一个if 声明。循环上方有:int select;。从int select = keyboard.nextInt(); 行中删除int 类型声明。
  • Also.... 从所有 if 语句代码块中删除 break; 语句。

标签: java loops if-statement structure


【解决方案1】:

你可以使用do-while 句法 做{在这里写代码} while(条件)

【讨论】:

    【解决方案2】:

    你的代码的问题是select的更新,我的意思是,它没有更新。

    您可以使用do-while 代替while,这是您的代码的修改版本:

    public class Main {
            public static void main(String... args) {
    
                System.out.println("Thank you for using the MCCH GeoCal program. \nWith this program, you will be able to find the area of three kinds of shapes.\nThis program uses doubles.");
                System.out.println();
    
                Scanner keyboard = new Scanner(System.in);
                int select = 0;
    
                do {
                    System.out.println("Please make a selection:");
                    System.out.println("1 - Area of a rectangle");
                    System.out.println("2 - Area of a triangle");
                    System.out.println("3 - Area of a circle");
                    System.out.println("4 - End program");
    
                    select = keyboard.nextInt();
                    if (select == 1) {
                        System.out.println("Enter the length of the rectangle.");
                        double length = keyboard.nextDouble();
                        System.out.println("Enter the width of the rectangle");
                        double width = keyboard.nextDouble();
                        System.out.println("The area of the rectangle is: " + rectArea(length, width));
                    } else if (select == 2) {
                        System.out.println("Enter the height of the triangle.");
                        double height = keyboard.nextDouble();
                        System.out.println("Enter the size of the base of the triangle.");
                        double base = keyboard.nextDouble();
                        System.out.println("The area of the triangle is: " + triArea(height, base));
                    } else if (select == 3) {
                        System.out.println("Enter the radius of the circle.");
                        double radius = keyboard.nextDouble();
                        System.out.println("The area of the circle is: " + cirArea(radius));
                    } else if (select < 1 || select > 4) {
                        System.out.println("Incorrect input.");
                    }
    
                } while (select != 4);
                System.out.println("Thank you for using this program.");
            }
    
            // This method is used to find the area of a rectangle.
            private static double rectArea(double length, double width) {
                return length * width;
            }
    
            private static double triArea(double height, double base) {
                return 0.5 * base * height;
            }
    
            private static double cirArea(double radius) {
                return Math.PI * Math.pow(radius, 2);
            }
    
        }
    

    【讨论】:

    • You can use a do-while instead of while ... 为什么?使用 while 循环有什么问题?
    • 没有错,只是你思考/编写逻辑的方式问题。在您的用例中,您确实需要在输入不是 4 时重复程序的特定部分,如果您使用 do-while 您编写的代码更少,并且无论如何都需要进行第一次执行
    • 不要误会我的意思,我并不是说你的做法是错误的。它没有任何问题,但是在编写原始代码时使用 while 循环也没有任何问题。此外,您必须向我展示如何编写 do { ... } while (...); 比编写 while (...) { ... } 更少代码。
    • 我从来没有说过使用while有什么问题,这是一个逻辑问题。循环内的块需要每次执行,直到输入为4。如果使用while,则需要在循环之前和结束时接收输入,而使用do-while只需将其添加到循环本身。就这么简单。
    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2017-06-25
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多