Может кто-нибудь помочь мне выяснить, почему заявление while не работает? Цикл останавливается после i = 3, но не останавливается, если continueSurvey = 0. Он работает, но он не выйдет из цикла, если я изменил continueSurvey на O. Даже если я вхожу в процессы, и я вижу, что переменная 0, цикл продолжается.Пока цикл в Java с несколькими условиями
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++;
}
}
}
}
Спасибо! Я закончил тем, что поставил оператор break в свой код. Я попытался переместить вопрос вне цикла for, но мне нужно было задать вопрос до того, как будет создан следующий ответчик. –
@ MichelleAnderson Да, это, вероятно, решение, которое имеет наибольший смысл :) Обязательно принимайте ответ StormeHawke, так как это поможет другим, у кого может быть аналогичная проблема. – Jordan