【发布时间】:2021-10-22 02:50:24
【问题描述】:
我正在尝试构建一个简单的猜谜游戏,您可以在其中选择数字范围。我想留在我的循环中,直到用户猜对了数字。在第一次错误猜测之后,while 循环不允许下一个输入。如何在变量匹配之前留在循环内?
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
test random = new test();
System.out.print("Please input a minumum amount: ");
int min = reader.nextInt();
System.out.print("Please input a maximum amount: ");
int max = reader.nextInt();
int answer = random.getRandom(min, max);
System.out.print("Please guess a number between you " + min + "-" + max + ": ");
int input = reader.nextInt();
while(input != answer) {
System.out.println("Guess again");
}
if(input == answer) {
System.out.println("You got it right!");
}
}
}
【问题讨论】:
-
您的陈述相互矛盾。 ...停留在我的循环中,直到用户猜出正确的数字 并且 while 循环在第一次错误猜测后不允许下一个输入。这两个怎么可能?
-
查看Java
do-whileloops