java - 我怎样才能让我的 do while 循环按照我想要的方式工作?

标签 java do-while

如果用户对“继续?:(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/

相关文章:

c# - 从列表中删除多余的项目时,枚举器陷入无限循环

c++ - 没有从等式中得到正确的答案。还有,无限循环

c++ - 用假值结束 C++ 中的 do while 循环

Java:使用一个函数返回几种不同类型的值

java - 正则表达式 - 用开始和结束引号替换带引号的字符串

java - 为什么我无法计算销售金额和销售税金额?

php - 使用 PHP 时,分配的变量在 do while 循环中不起作用

java - 从最高到最低的顺序和位置从另一个 arraylist java 更改

java - 使用 SELECT * FROM [table] 时 hsqldb 非常慢

c# - 从 do while 语句接收用户输入