提问人:filocoderist 提问时间:6/13/2022 最后编辑:azrofilocoderist 更新时间:10/4/2023 访问量:53
除了第二个 else-if 语句外,所有条件都有效,如何纠正此问题?
all of the condition are working except on the second else-if statement, how to correct this?
问:
我正在学习 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;
}
当我运行代码并尝试通过达到 int i 的最大数量(即 5)来使游戏失败时,应该出现第二个 else if 语句,但它不起作用。
我只是不明白它没有读取 (i==5) 条件的原因,除此之外所有其他条件都在工作。
答:
好吧,if-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;
}
即使 ,如果满足 或 中的任何一个,其余的语句 including 也会被忽略。i == 5
guess < numberToGuess
guess > 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;
}
所以,看起来是这样,因为在检查你是否处于正确的循环条件下之前,有变量的检查
...
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;
}
这样,您可以先检查循环条件,然后检查输入条件
这就是工作原理,您的值将低于或高于猜测值,因此第一个 2 个条件的一个为真,然后不会检查其他条件if/else if/else
首先设置停止条件
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;
}
}
使用 if-else 语句时的经验法则是 which ever 条件 将满足到最早,它将进入该声明并 之后,它将在 if-else 条件之外执行代码。
所以在你的情况下,if-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,如果 guess < numberToGuess 或 guess > numberToGuess 满足,它将执行其中任何一个,而不是 i==5。因此,只要想想您想要赋予更多优先级的逻辑。
评论