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

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

最佳答案

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

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

关于java - 使用 Java 中的用户输入循环程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33190028/

10-12 16:56