如何解决卡在Java猜测程序中的if-else嵌套语句陷入了无限循环
/ * 此应用程序使用数字玩Hi-Lo猜谜游戏。该程序会选择一个介于1到100(含)之间的随机数,然后反复提示用户猜测该数字。
每次猜测时,都会通知用户他们是正确的还是猜测是不正确的(无论是高还是低)。
程序将继续接受猜测,直到用户正确猜测或选择退出为止。它还将计数并报告用户的猜测数目,以正确猜测所选的数目。如果猜测值超出范围(1到100),则将建议用户更正其猜测值,并且该猜测值不会计算在内。在每个游戏结束时(通过退出或正确的猜测),将要求用户再次玩。如果他们选择继续玩其他游戏,程序将循环播放直到用户选择停止为止。 * /
System.out.println("Let's play a guessing game!");
System.out.println("Pick a number between " + MIN + " and " + MAX +
" (inclusive). ");
guess = scan.nextInt();
while
{
if (guess < MIN || guess > MAX)
{
System.out.println("Your guess is out of range!");
System.out.println("Pick a number between " + MIN + " and " + MAX +
" (inclusive). ");
}
count++;
else if(guess == answer)
{
System.out.println("CONGRATULATIONS! The number was " + answer +
". You guessed correctly in " + count + " tries!");
System.out.println("Would you like to play again?");
System.out.print("Type \"Y\" to play and \"N\" to quit. ");
String play = scan.nextLine();
if(play.equalsIgnoreCase("Y"))
{
System.out.println("Game on!");
}
else if(play.equalsIgnoreCase("N"))
{
System.out.println("Thanks for playing! Good-bye.");
return;
}
}
else if (guess < answer)
{
System.out.println("Sorry,your guess " + guess + " is too low. "
+ "Try again!");
}
else if (guess > answer)
{
System.out.println("Sorry,your guess " + guess + " is too high. "
+ "Try again!");
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。