除第二个语句外,所有条件都可以正常工作,如何纠正此问题?
我正在学习java,我不知道是什么使我的代码不阅读我的其他条件(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;
}
当我运行代码并尝试通过达到最大int i的最大int i(5是5 ) ,第二个如果要出现语句,但它不起作用。
我只是不明白为什么它没有阅读(i == 5)条件,除此之外,所有其他条件都在起作用。
I am learning Java and I do not know what makes my code not reading my else-if condition (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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用IF-ELSE语句时的拇指规则是
将对最早的感到满意,它将进入该声明,
之后,它将在IF-ELSE条件之外执行代码。
因此,就您而言,如果else语句效法
满足陈述,其余的人都被忽略了。拿
作为一个例子。
即使i == 5,数字或猜测&gt;
满足了numberToguess,它将执行这些而不是执行
i == 5。
因此,请考虑一下您想给予更多优先级的逻辑。
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.
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. Take
yours as an example.
Even when i == 5, if either of guess < numberToGuess or guess >
numberToGuess is satisfied, it will execute either of these instead of
i==5.
So just think of the logic that you want to give more priority.
好吧,如果else语句会这样起作用:当满足任何一个语句时,其余的人都将被忽略。以您的榜样为例。
即使
i == 5
,如果猜测&lt; numberToguess
或猜测&gt;满足了numberToguess
,其余语句(包括)(i == 5)
被忽略。要解决此问题,只需将
i == 5
作为第一个检查的条件,因为如果i == 5
感到满意,我们都不应该考虑猜测。我还猜想您想在游戏结束时突破循环,因此我还添加了
break
语句。Well, if-else statements work like this: when any one of the statements are satisfied, the rest of them are ignored. Take yours as an example.
Even when
i == 5
, if either ofguess < numberToGuess
orguess > numberToGuess
are satisfied, the rest of the statements including(i == 5)
are ignored.To fix this, simply add
i == 5
as the first condition to check, because ifi == 5
is satisfied we shouldn't be considering the guess anyway.I'm also guessing you want to break out of the loop if the game ends, so I added a
break
statement as well.因此,似乎是这样,因为在检查您是否处于正确的循环条件下之前,有
执行变量的检查。因此,要修复错误,只需将检查i == 5移至第一个分支条款即可。代码应像接下来一样,
以便您首先检查循环条件并在
So, that appears to be so, because before checking if you are under the right loop condition, there are checks of variables
Which are executed. So, to fix the bug, just move your check i == 5 to be the first branching clause. The code should look like next
This way you check the loop condition first and input condition after
这就是
如果/else 有效的话,您的价值将比猜测低或更高,因此2个第一个条件将是正确的,那么任何其他人都不会被检查
设置停止起初条件
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 checkedSet the stop condition at first