我怎么能按照我想要的方式使我的do循环工作?

时间:2016-03-29 15:56:08

标签: java do-while

如果用户对“Continue?:( y / n)”提示符回答是,我试图让我的代码循环提示输入矩形长度和宽度。

目前正在循环“继续?:(y / n)”部分而不是“输入长度:”“输入宽度:”< / em>部分。 任何帮助表示赞赏!

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"));
    }
}

1 个答案:

答案 0 :(得分:3)

Scanner sc = new Scanner(System.in);
String response;
do {

System.out.println("Enter Length: ");

为什么循环体外的任何内容(do {} while (...)之间的内容)会重复?