gpt4 book ai didi

java - Java 中的 While 循环具有多个条件

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:51 25 4
gpt4 key购买 nike

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

最佳答案

您需要在 for 循环中添加一个 break。浏览器,

if(continueSurvey == 0)
break;

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

关于java - Java 中的 While 循环具有多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18085974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com