具有多个条件的 Java 中的 while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18085974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
While loop in Java with Multiple conditions
提问by Goofy_1969
Can someone help me figure out why the while statement isn't working? The loop does stop after i = 3 but won't stop if continueSurvey = 0. It runs but it won't quit the loop if I change continueSurvey to O. Even if I step into the processes and I can see that the variable is 0, the loop continues.
有人可以帮我弄清楚为什么 while 语句不起作用吗?循环会在 i = 3 后停止,但如果 continueSurvey = 0 则不会停止。它会运行,但如果我将 continueSurvey 更改为 O,它不会退出循环。即使我进入进程并且我可以看到变量是0,循环继续。
import java.util.Scanner;
public class SurveyConductor
{
public static void main(String[] args)
{
Survey a = new Survey();
a.display();
a.enterQuestions();
int continueSurvey = 1;
int i = 0;
while ((continueSurvey != 0) && (i < 3))
{
for (int row = a.getRespID(); row < 3; row++)
{
System.out.println("Respondent " + (row+1) + " Please tell us how you would rate our: ");
for (int col = 0; col < 3; col++)
{
Scanner input = new Scanner(System.in);
System.out.println(a.presentQuestion(col) + ": ");
System.out.println("Enter your response (1-Strongly Disagree, 2-Disagree, 3-Neutral, 4-Agree, 5-Strongly Agree): ");
int response = input.nextInt();
if ((response < 1) || (response >5))
{
while ((response < 1) || (response > 5))
{
System.out.println("Your response must be between 1 and 5. Please try again.");
System.out.println(a.presentQuestion(col) + ": ");
System.out.println("Enter your response (1-Strongly Disagree, 2-Disagree, 3-Neutral, 4-Agree, 5-Strongly Agree): ");
response = input.nextInt();
}
}
a.logResponse(row,col,response);
System.out.println();
}
a.displaySurveyResults();
System.out.println();
System.out.println("The top rated question is Question #" + a.topRatedQuestion() + ".");
System.out.println("The bottom rated question is Question #" + a.bottomRatedQuestion() + ".");
System.out.println();
Scanner input2 = new Scanner(System.in);
System.out.println("Are there any more repondents (0 - No, 1 - Yes): ");
continueSurvey = input2.nextInt();
a.generateRespondentID();
i++;
}
}
}
}
回答by Jordan
The part where you ask if the user wants to continue is inside this for loop
您询问用户是否要继续的部分在此 for 循环内
for (int row = a.getRespID(); row < 3; row++)
not just your while loop. This means it will keep asking until the for loop is done, only quitting when it finally gets back around to the while loop condition.
不仅仅是你的 while 循环。这意味着它会一直询问直到 for 循环完成,只有当它最终回到 while 循环条件时才会退出。
回答by benny
if you want to exit the loop if either continueSurvey is 0 OR i=3 you have to write the while loop like this:
如果要在 continueSurvey 为 0 或 i=3 时退出循环,则必须像这样编写 while 循环:
while((continueSurvey != 0) || (i < 3)) {
...
}
the && (and) operator symbolises that both conditions have to be true in order for the loop to exit not one of them (|| or).
&& (and) 运算符表示两个条件都必须为真,循环才会退出其中之一(|| 或)。
回答by StormeHawke
You need to add a break
inside your for
loop. IE,
您需要break
在for
循环中添加一个。IE,
if(continueSurvey == 0)
break;
This will exit the for
loop and allow the while
loop to exit.
这将退出for
循环并允许while
循环退出。
回答by Lajos Arpad
Your condition in the while loop is:
你在 while 循环中的条件是:
((continueSurvey != 0) && (i < 3))
which means that the inner block of the while loop will be executed if and only if continuSurvey != 0 and i < 3 in the same time. You have inner loops which have different conditions. I would search for the problem in the inner loops using a debugger. If this answer is not enough for you, then please specify what would you want to achieve.
这意味着当且仅当 continuSurvey != 0 和 i < 3 同时执行时,while 循环的内部块才会被执行。您有具有不同条件的内部循环。我会使用调试器在内部循环中搜索问题。如果这个答案对您来说还不够,那么请说明您想要实现的目标。