[英]all of the condition are working except on the second else-if statement, how to correct this?
I am learning Java and I do not know what makes my code not reading my else-if condition (i == 5)我正在学习 Java,但我不知道是什么让我的代码无法读取 else-if 条件(i == 5)
int i;
for (i = 1; i <= 5; i++) {
int guess = scanner.nextInt();
System.out.println("Attempt: " + i +"/5");
if (guess < numberToGuess) {
System.out.println("Higher");
} else if (guess > numberToGuess) {
System.out.println("Lower");
} else if (i == 5) {
System.out.println("You failed! The correct answer is " + numberToGuess);
} else {
System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
break;
}
when i'm running the code and try to fail the game by reaching the maximum number of int i which is 5, the second else if statement should appear but it is not working.当我运行代码并尝试通过达到最大 int i 数(即 5)使游戏失败时,应该出现第二个 else if 语句,但它不起作用。
I just don't get the reason why it is not reading the (i==5) condition, all of the other conditions are working except that.我只是不明白为什么它没有读取 (i==5) 条件,除此之外所有其他条件都有效。
Well, if-else statements work like this: when any one of the statements are satisfied, the rest of them are ignored.好吧,if-else 语句的工作原理是这样的:当满足任何一个语句时,其余的将被忽略。 Take yours as an example.以你的为例。
if (guess < numberToGuess) {
System.out.println("Higher");
} else if (guess > numberToGuess) {
System.out.println("Lower");
} else if (i == 5) {
System.out.println("You failed! The correct answer is " + numberToGuess);
} else {
System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
break;
}
Even when i == 5
, if either of guess < numberToGuess
or guess > numberToGuess
are satisfied, the rest of the statements including (i == 5)
are ignored.即使在i == 5
时,如果guess < numberToGuess
或guess > numberToGuess
中的任何一个都满足,则包括(i == 5)
在内的其余语句都将被忽略。
To fix this, simply add i == 5
as the first condition to check, because if i == 5
is satisfied we shouldn't be considering the guess anyway.要解决这个问题,只需添加i == 5
作为要检查的第一个条件,因为如果i == 5
满足,我们无论如何都不应该考虑猜测。
I'm also guessing you want to break out of the loop if the game ends, so I added a break
statement as well.我也猜想你想在游戏结束时跳出循环,所以我也添加了一个break
语句。
if (i == 5) {
System.out.println("You failed! The correct answer is " + numberToGuess);
break;
} else if (guess < numberToGuess) {
System.out.println("Higher");
} else if (guess > numberToGuess) {
System.out.println("Lower");
} else {
System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
break;
}
So, that appears to be so, because before checking if you are under the right loop condition, there are checks of variables所以,看起来是这样,因为在检查你是否处于正确的循环条件下之前,会检查变量
...
if (guess < numberToGuess) {
System.out.println("Higher");
} else if (guess > numberToGuess) {
System.out.println("Lower");
}
....
Which are executed.哪些被执行。 So, to fix the bug, just move your check i == 5 to be the first branching clause.因此,要修复该错误,只需将您的检查 i == 5 移动为第一个分支子句。 The code should look like next代码应该如下所示
if (i == 5) {
System.out.println("You failed! The correct answer is " + numberToGuess);
} else if (guess > numberToGuess) {
System.out.println("Lower");
} else if (guess < numberToGuess) {
System.out.println("Higher");
} else {
System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
break;
}
This way you check the loop condition first and input condition after这样你首先检查循环条件,然后输入条件
That is how if/else if/else
works, your value will be either lower or higher than the guess, so one for the 2 first condition will be true, then no other will be checked这就是if/else if/else
的工作原理,您的值将低于或高于猜测值,因此第 2 个条件为真,则不会检查其他值
Set the stop condition at first首先设置停止条件
int nb_tries = 5;
for (i = 1; i <= nb_tries; i++) {
System.out.print("Attempt: " + i + "/" + nb_tries + " => ");
int guess = scanner.nextInt();
if (i == nb_tries) {
System.out.println("You failed! The correct answer is " + numberToGuess);
} else if (guess < numberToGuess) {
System.out.println("Higher");
} else if (guess > numberToGuess) {
System.out.println("Lower");
} else {
System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
break;
}
}
The Thumb rule while using if-else statement is which ever condition will satisfied to the earliest , it will go into that statement and after that it will execute the code outside of if-else condition.使用 if-else 语句时的 Thumb 规则是最早满足哪个条件,它将进入该语句,然后在 if-else 条件之外执行代码。
So in your case Well, if-else statements work like this: when any one of the statements are satisfied, the rest of them are ignored.因此,在您的情况下,if-else 语句的工作方式如下:当满足任何一个语句时,其余的将被忽略。 Take yours as an example.以你的为例。
if (guess < numberToGuess) {
System.out.println("Higher"); } else if (guess > numberToGuess) {
System.out.println("Lower"); } else if (i == 5) {
System.out.println("You failed! The correct answer is " + numberToGuess); } else {
System.out.println("You won! Your answer is " + numberToGuess + " which is correct!");
break; }
Even when i == 5, if either of guess < numberToGuess or guess > numberToGuess is satisfied, it will execute either of these instead of i==5.即使当 i == 5 时,如果guess < numberToGuess 或guess > numberToGuess 中的任何一个满足,它将执行其中任何一个而不是i==5。 So just think of the logic that you want to give more priority.所以只要想想你想给予更多优先级的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.