我如何循环此决策结构?

问题描述 投票:0回答:1

[抱歉,我很难得到这个循环,尽管我会停止调整它并询问。我希望决策结构循环,我希望它通过输入“ 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;
    }
}
java loops if-statement structure
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.