【问题标题】:While loop in Java with Multiple conditions具有多个条件的Java中的while循环
【发布时间】:2013-08-07 19:14:35
【问题描述】:

有人可以帮我弄清楚为什么 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++;
                }
            } 
    }
}

【问题讨论】:

    标签: java loops while-loop conditional-statements


    【解决方案1】:

    您需要在for 循环中添加一个break。即,

    if(continueSurvey == 0)
        break;
    

    这将退出for 循环并允许while 循环退出。

    【讨论】:

    • 不客气!如果我的回答解决了您的问题,请考虑将其标记为已接受的答案(数字下方左侧的复选标记)
    【解决方案2】:

    询问用户是否要继续的部分在这个 for 循环中

    for (int row = a.getRespID(); row < 3; row++)
    

    不仅仅是你的 while 循环。这意味着它会一直询问直到 for 循环完成,只有当它最终回到 while 循环条件时才会退出。

    【讨论】:

    • 谢谢!我最终将 break 语句放在我的代码中。我尝试将问题移到 for 循环之外,但我需要它在生成下一个受访者之前提出问题。
    • @MichelleAnderson 是的,这可能是最有意义的解决方案 :) 请务必接受 StormeHawke 的回答,因为这将有助于其他可能遇到类似问题的人。
    【解决方案3】:

    你在while循环中的条件是:

    ((continueSurvey != 0) && (i < 3))
    

    这意味着当且仅当 continuSurvey != 0 和 i

    【讨论】:

      【解决方案4】:

      如果您想在 continueSurvey 为 0 或 i=3 时退出循环 你必须像这样编写while循环:

      while((continueSurvey != 0) || (i < 3)) {
          ...
      }
      

      && (and) 运算符表示两个条件都必须为真才能使循环不退出其中之一(|| 或)。

      【讨论】:

      • 没有。如果 continueSurvey 为 0 且 i = 2,在您的示例中,条件将评估为 true,这意味着它不会退出 while 循环。 && 表示如果其中任何一个为假,它将退出循环。
      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2013-09-29
      • 2015-02-14
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      相关资源
      最近更新 更多