gpt4 book ai didi

java - 我如何循环这个决策结构?

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:42 25 4
gpt4 key购买 nike

抱歉,我很难得到这个循环,我想我会停止调整它并询问。我希望决策结构循环,我希望它通过输入“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;
}
}

最佳答案

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

关于java - 我如何循环这个决策结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60158551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com