我尝试创建基本的数字查找游戏(1-100)。 但程序总是在第二次尝试后终止。
代码:
Random rnd = new Random();
int rndm = rnd.nextInt(100);
int tryn;
Scanner sc = new Scanner(System.in);
System.out.print("Guess: ");
tryn = sc.nextInt();
if (tryn == rndm) {
System.out.println("You win.");
sc.close();
}
else if (tryn > rndm) {
System.out.println("Too high.");
System.out.print("Guess again: ");
tryn = sc.nextInt();
}
else {
System.out.println("Too low.");
System.out.print("Guess again: ");
tryn = sc.nextInt();
}
当我运行如下输出时:
<terminated> test2 [Java Application]...
Guess: 30
Too low.
Guess again: 50
为什么我的程序不循环?感谢您的帮助。
最佳答案
在代码中添加循环...
while(tryn != rndm){
关于Java if 语句终止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33265882/