如何在循环内的嵌套 if 语句中结束 while 循环? (可能很容易)
How to end while-loop in a nested-if statement within the loop? (probably easy)
下面的代码是猜数游戏的一部分,在这个游戏中,计算机会生成一个用户指定范围内的随机数。在这里,我试图将用户限制为 10 次猜测。如果he/she超过10,则游戏结束:
int t1 = 0;//stores # of guesses
while(true){//loop begins
t1++;//increments each iteration to represent # of guesses
if(g1!=n){//if the guess is incorrect... (n = # user is trying to guess)
if(t1>10){//# of guesses cannot exceed 10
System.out.println("\nGAME OVER\nYou have exceeded the max # of tries!");
break;}//game ends if user exceeds max # of attempts
if(g1<n){System.out.println("\nGuess Higher!\n" + t1 + "attempt(s) so far");
continue;}//guess is too low
if(g1>n){System.out.println("\nGuess Lower!\n" + t1 + " attempt(s) so far");
continue;}}//guess is too high (loop ends)
我收到的输出只有 1/2 正确。例如,假设计算机生成了 1-100 范围内的数字 12。在用户第 10 次(最后一次)猜测时,它将打印:"GAME OVER...";但是,如果用户的猜测太低或太高,它也会打印出来,这是我不想要的。
我需要做哪些更改才能更正此错误?我认为这与 nested-if 中的 'break' 语句有关。
您的问题似乎有效。代码是正确的,它正在做它应该做的事情。但是,编写相同代码的更好、更有效的方法是:
// run a for loop that will let user guess 10 times
for (int i = 0; i < 10; i++)
{
// see if the guessed number is incorrect
if (g1 != n)
{
// check whats wrong with the number entered
// number of guesses cannot exceed 10
if ((i+1) >= 10)
{
System.out.println("\nGAME OVER\nYou have exceeded the max # of tries!");
}
else if (g1 < n)
{
// if user's guess is less than the number
System.out.println("\nGuess Higher!\n" + (i+1) + "attempt(s) so far");
}
else if (g1 > n)
{
// if user's guess is greater than the number
System.out.println("\nGuess Lower!\n" + (i+1) + " attempt(s) so far");
}
}
} // for loop ends
下面的代码是猜数游戏的一部分,在这个游戏中,计算机会生成一个用户指定范围内的随机数。在这里,我试图将用户限制为 10 次猜测。如果he/she超过10,则游戏结束:
int t1 = 0;//stores # of guesses
while(true){//loop begins
t1++;//increments each iteration to represent # of guesses
if(g1!=n){//if the guess is incorrect... (n = # user is trying to guess)
if(t1>10){//# of guesses cannot exceed 10
System.out.println("\nGAME OVER\nYou have exceeded the max # of tries!");
break;}//game ends if user exceeds max # of attempts
if(g1<n){System.out.println("\nGuess Higher!\n" + t1 + "attempt(s) so far");
continue;}//guess is too low
if(g1>n){System.out.println("\nGuess Lower!\n" + t1 + " attempt(s) so far");
continue;}}//guess is too high (loop ends)
我收到的输出只有 1/2 正确。例如,假设计算机生成了 1-100 范围内的数字 12。在用户第 10 次(最后一次)猜测时,它将打印:"GAME OVER...";但是,如果用户的猜测太低或太高,它也会打印出来,这是我不想要的。
我需要做哪些更改才能更正此错误?我认为这与 nested-if 中的 'break' 语句有关。
您的问题似乎有效。代码是正确的,它正在做它应该做的事情。但是,编写相同代码的更好、更有效的方法是:
// run a for loop that will let user guess 10 times
for (int i = 0; i < 10; i++)
{
// see if the guessed number is incorrect
if (g1 != n)
{
// check whats wrong with the number entered
// number of guesses cannot exceed 10
if ((i+1) >= 10)
{
System.out.println("\nGAME OVER\nYou have exceeded the max # of tries!");
}
else if (g1 < n)
{
// if user's guess is less than the number
System.out.println("\nGuess Higher!\n" + (i+1) + "attempt(s) so far");
}
else if (g1 > n)
{
// if user's guess is greater than the number
System.out.println("\nGuess Lower!\n" + (i+1) + " attempt(s) so far");
}
}
} // for loop ends