除第二个语句外,所有条件都可以正常工作,如何纠正此问题?

发布于 2025-02-06 21:15:08 字数 702 浏览 2 评论 0原文

我正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

智商已欠费 2025-02-13 21:15:09

使用IF-ELSE语句时的拇指规则是
将对最早的
感到满意,它将进入该声明,
之后,它将在IF-ELSE条件之外执行代码。

因此,就您而言,如果else语句效法
满足陈述,其余的人都被忽略了。拿
作为一个例子。

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; }

即使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.

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.
So just think of the logic that you want to give more priority.

无尽的现实 2025-02-13 21:15:08

好吧,如果else语句会这样起作用:当满足任何一个语句时,其余的人都将被忽略。以您的榜样为例。

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;
}

即使i == 5,如果猜测&lt; numberToguess猜测&gt;满足了numberToguess,其余语句(包括)(i == 5)被忽略。

要解决此问题,只需将i == 5作为第一个检查的条件,因为如果i == 5感到满意,我们都不应该考虑猜测。

我还猜想您想在游戏结束时突破循环,因此我还添加了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;
}

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.

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.

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'm also guessing you want to break out of the loop if the game ends, so I added a break statement as well.

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;
}
独木成林 2025-02-13 21:15:08

因此,似乎是这样,因为在检查您是否处于正确的循环条件下之前,有

... 
if (guess < numberToGuess) {
            System.out.println("Higher");
        } else if (guess > numberToGuess) {
                System.out.println("Lower");
        } 
....

执行变量的检查。因此,要修复错误,只需将检查i == 5移至第一个分支条款即可。代码应像接下来一样,

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;
}

以便您首先检查循环条件并在

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. 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

可爱暴击 2025-02-13 21:15:08

这就是如果/else 有效的话,您的价值将比猜测低或更高,因此2个第一个条件将是正确的,那么任何其他人都不会被检查

设置停止起初条件

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;
    }
}

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文