如果用户对“继续?:(y/n)”提示回答"is",我会尝试让我的代码循环提示输入矩形长度和宽度。
当前正在循环“继续?:(y/n)”部分,而不是“输入长度:”和“输入宽度:” 部分。 如有任何帮助,我们将不胜感激!
import java.util.Scanner;
public class Day1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Grand Circus' Room Detail Generator!");
double length = 0.0;
double width = 0.0;
double area = 0.0;
double perimeter = 0.0;
System.out.println("Enter Length: ");
length = scanner.nextInt();
System.out.println("Enter Width: ");
width = scanner.nextInt();
area = length * width;
perimeter = 2 * (length + width);
System.out.println("Area: " + area);
System.out.println("Perimeter: " + perimeter);
Scanner sc = new Scanner(System.in);
String response;
do {
System.out.print("Continue? (y/n):");
response = sc.next();
} while (response.equals("y") || response.equals("Y"));
}
}
最佳答案
放置
Scanner sc = new Scanner(System.in);
String response;
do {
之前
System.out.println("Enter Length: ");
为什么循环体之外的内容(do {
和 } while (...)
之间的内容)会重复?
关于java - 我怎样才能让我的 do while 循环按照我想要的方式工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36289253/