gpt4 book ai didi

java - 将 'While' 循环添加到我的程序中

转载 作者:行者123 更新时间:2023-12-01 08:52:09 26 4
gpt4 key购买 nike

我正在开发一个程序,该程序根据用户输入的字母来计算圆形 (C)、正方形 (S) 或矩形 (R) 的面积。我已经测试过了,效果很好;代码如下:

import java.util.Scanner;

public class TestLoops {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("What is your shape? Enter C for circle, S for " +
"square, R for rectangle, or X to exit: ");
String Shape = input.nextLine();

if (Shape.equals("C")) {
System.out.println("What is your circle's radius?: ");
double Radius = input.nextDouble();
double cFormula = (3.14 * Radius * Radius);
System.out.println("Your circle's area = " + cFormula);
}
else if (Shape.equals("S")) {
System.out.println("What is the length of your shape's sides?: ");
double Side = input.nextDouble();
double sFormula = (Side * Side);
System.out.println("Your square's area = " + sFormula);
}
else if (Shape.equals("R")) {
System.out.println("What is your rectangle's height?: ");
double Height = input.nextDouble();
System.out.println("What is your rectangle's width?: ");
double Width = input.nextDouble();
double rFormula = (Height * Width);
System.out.println("Your rectangle's area = " + rFormula);
}

}

}

现在,我想做的是向程序添加一个循环。例如,如果用户输入 C 代表圆,并输入数字 22 代表半径,他们会得到答案,但我希望程序再次循环回到开头,以便它询问用户“你的形状是什么” ?...”。另外,如果用户输入 X 而不是 C、S 或 R,我希望程序退出,但我也不知道如何添加它。

我知道我需要添加一个“while”循环,但我希望有人能给我指出正确的方向,因为我不知道在哪里插入这部分代码。我是否要在代码开头的某个位置、最后一个“if else”语句之后添加“while”循环,或者...另外,我实际上不确定要输入什么内容。应该是这样的,

while (Shape == C, S, R) {
....?

编码社区中的任何人都将不胜感激任何帮助或指示!我也将继续自己编写此代码。

最佳答案

我会选择do, while

因此,在完成所设置的条件时,程序总是会执行某些操作,因此您希望程序看起来像这样:

public class TestLoops {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean thisB = false; /*this is the guy who will tell the loop to stop the execution when the user inserts X*/
String shape;
do{
System.out.println("What is your shape? Enter C for circle, S for " +
"square, R for rectangle, or X to exit: ");
shape = input.next();

if(shape.equalsIgnoreCase("C") || shape.equalsIgnoreCase("S") || shape.equalsIgnoreCase("R")) {
if (shape.equals("C")) {
System.out.println("What is your circle's radius?: ");
double Radius = input.nextDouble();
double cFormula = (3.14 * Radius * Radius);
System.out.println("Your circle's area = " + cFormula);
} else if (shape.equals("S")) {
System.out.println("What is the length of your shape's sides?: ");
double Side = input.nextDouble();
double sFormula = (Side * Side);
System.out.println("Your square's area = " + sFormula);
} else if (shape.equals("R")) {
System.out.println("What is your rectangle's height?: ");
double Height = input.nextDouble();
System.out.println("What is your rectangle's width?: ");
double Width = input.nextDouble();
double rFormula = (Height * Width);
System.out.println("Your rectangle's area = " + rFormula);
}
}
else if (shape.equalsIgnoreCase("X")) thisB = true;/*or in other words: stop*/
}
while(!thisB);
}

}

需要考虑的事情:

1) 命名约定,始终使用 camelCase 以小写字母开头变量名称,在示例中 shape 以大写字母开头

2) 在 while 循环中时,仅使用 next() 而不是 nextLine() 来获取后者的值将在 System.out.Println 中重复问题。

3) 执行此操作的最佳方法是将所有 if 子句放入一个方法中,并使用 Scanner 输入中的参数调用它。更好的是每个形状都有一个方法,因为根据请求事情可能会变得很复杂

关于java - 将 'While' 循环添加到我的程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322193/

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