简体   繁体   中英

While loop in Java with Multiple conditions

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.

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

You need to add a break inside your for loop. IE,

if(continueSurvey == 0)
    break;

This will exit the for loop and allow the while loop to exit.

The part where you ask if the user wants to continue is inside this for loop

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.

Your condition in the while loop is:

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

if you want to exit the loop if either continueSurvey is 0 OR i=3 you have to write the while loop like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM