我只想在用户输入“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"));
}
答案 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"));
}
您需要阅读用户输入的选择。