使用Java中的用户输入循环程序

时间:2015-10-17 18:20:22

标签: java loops while-loop

我只想在用户输入“yes”的情况下循环此程序。我想我会在整个程序中使用do/while循环,但没有任何反应。这是我的代码:

public static void main(String[] args) {
    int num1, i;
    Scanner Scan = new Scanner(System.in);
    do {
        do {
            System.out.print("Enter a number between 20 and 30 ---> ");
            num1 = Scan.nextInt();
        } while (num1 > 30 || num1 < 20);

        for (i = num1; i >= 20; i--) {
            System.out.print(i + " ");
        }
        System.out.println("");
    } while (Scan.equals("yes"));
}

1 个答案:

答案 0 :(得分:2)

public static void main(String[] args) {
    int num1, i;
    String choice;
    Scanner Scan = new Scanner(System.in);

    do {
        do {
            System.out.print("Enter a number between 20 and 30 ---> ");
            num1 = Scan.nextInt();
        } while (num1 > 30 || num1 < 20);

        for (i = num1; i >= 20; i--) {

            System.out.print(i + " ");
        }

        System.out.println("");
        choice = Scan.next();
    }
    while (choice.equals("yes"));
}

您需要阅读用户输入的选择。