【发布时间】:2014-11-17 18:33:54
【问题描述】:
我的代码中有一个无限循环,我对如何修复它感到困惑。如果有人能指出我正确的方向或帮助我,那将非常感激。谢谢。 方法如下:
public static void random(){
int rand = (int) (Math.random() * 11);
Scanner input = new Scanner(System.in);
System.out.print("Enter a guess: ");
int guess = input.nextInt();
if (guess == rand){
do {
System.out.println("Your guess is " + guess + ".");
System.out.println("Your guess is correct! Yay!");
} while (guess == rand);
}
if (guess < rand){
do {
System.out.println("Your guess is " + guess + ".");
System.out.println("Your guess is too low. Guess again");
} while (guess!= rand);
if (guess > rand){
do {
System.out.println("Your guess is " + guess + ".");
System.out.println("Your guess is too high. Guess again");
} while (guess > rand);
}
【问题讨论】:
-
如果猜测太低,为什么打印消息会改变?
-
使用调试器单步调试您的代码。应该很容易发现。
-
@Andrew 我真的怀疑这个学生被教导使用调试器
-
没错。 do...while 循环不包含任何更改
guess的代码。
标签: java infinite-loop do-while