java - 在java中的while循环之前添加程序退出
问题描述
如果在循环开始之前输入 -1 以终止循环,我无法让程序停止并显示下面的消息。如果输入了不同的整数,该程序也不会流入 while 循环。我似乎把我的大括号放错了地方,因为最后两个出错了。
public static void main(String[] args)
{
double score;
double total = 0.0;
double average;
int scoreCount = 0;
// create the Scanner object. Name it stdin
Scanner stdin = new Scanner(System.in);
// title at the top of the output
System.out.println (" score report");;
do{
// read the first score
System.out.printf ("Enter a score (1-100, -1 to quit)"
+ ": ", scoreCount);
score = stdin.nextDouble();
if (score == -1)
{
System.out.println ("Nothing entered.");
}
while((score = stdin.nextDouble()) != -1.0)
{
if (score<-1 || score>100)
{
System.out.println ("Illegal score. Try again");
System.out.printf ("Enter a score (1-100, -1 to quit)"
+ ": ", scoreCount);
}
else
{
System.out.printf ("Enter a score (1-100, -1 to quit)"
+ ": ", scoreCount);
scoreCount++;
total += score;
}
// end of for loop
average = total / scoreCount; //equation
System.out.printf ("\nThe average score for %d students is %8.2f\n",
scoreCount, average);
}
} // end of main
} // 类定义结束
解决方案
在 do 循环结束时需要一个“while”条件。这就是为什么它被称为 do-while 循环。如果没有 while 条件,您应该会收到编译时错误。这是一个例子:
double score;
double total = 0.0;
double average;
int scoreCount = 0;
// create the Scanner object. Name it stdin
Scanner stdin = new Scanner(System.in);
// title at the top of the output
System.out.println ("Score Report");
do{
// read the first score
System.out.println("Enter a score (0-100 or -1 to quit)"
+ ": " + scoreCount);
score = stdin.nextDouble();//Retrieve the score.
if(score == -1) {
System.out.println("Bye!");
}
if (score<-1 || score>100)//Here is the if statement that makes the user enter another score if it is illegal
{
System.out.println("Illegal score. Try again");
continue; //A continue statement will start the loop over again from the top!
}
else if(score >= 0 && score <= 100 && score != -1)
{
scoreCount++;
total += score;
}
// end of for loop
average = total / scoreCount; //equation
System.out.println();
System.out.printf ("\nThe average score for %d students is %8.2f\n",
scoreCount, average);
System.out.println();
}while(score != -1); //Runs "while" the score != -1
以下是该程序可能输出的示例:
Score Report
Enter a score (0-100 or -1 to quit): 0
50.0
The average score for 1 students is 50.00
Enter a score (0-100 or -1 to quit): 1
50.0
The average score for 2 students is 50.00
Enter a score (0-100 or -1 to quit): 2
-90
Illegal score. Try again
Enter a score (0-100 or -1 to quit): 2
50.0
The average score for 3 students is 50.00
Enter a score (0-100 or -1 to quit): 3
-1
Bye!
The average score for 3 students is 50.00
正如您在此处看到的,您的 if 语句更改为:
if (score<-1 || score>100)//Here is the if statement that makes the user enter another score if it is illegal
{
System.out.println("Illegal score. Try again");
continue;
}
continue 语句将强制循环从头开始,而不执行循环中的其余代码。这将帮助您避免无效输入。在 do 循环结束时,您还需要这个 while 条件:
}while(score != -1);
现在循环只会在分数不等于负 1 时继续。如果分数等于 -1,您还可以在代码中通知用户他们正在退出程序:
if(score == -1) {
System.out.println("Bye!");
}
如果输入的数字在 0 到 100 的范围内,您可以将 else 语句更改为 else if 语句以执行,否则 -1 条目将被计为分数:
else if(score >= 0 && score <= 100 && score != -1)
{
scoreCount++;
total += score;
}
// end of for loop
你也没有像你说的那样退出你的循环。每次迭代循环时都会计算平均值。将显示平均值的代码放在循环之外:
}while(score != -1);
average = total / scoreCount; //equation
System.out.println();
System.out.printf ("\nThe average score for %d students is %8.2f\n",
scoreCount, average);
现在输出将如下所示:
Score Report
Enter a score (0-100 or -1 to quit): 0
50.0
Enter a score (0-100 or -1 to quit): 1
70.0
Enter a score (0-100 or -1 to quit): 2
90.0
Enter a score (0-100 or -1 to quit): 3
-1
Bye!
The average score for 3 students is 70.00
如果您希望仅在输入 -1 作为第一个选项时显示消息,请执行以下操作:
if(score == -1 && scoreCount == 0) {
System.out.println("Bye!");
}
推荐阅读
- stored-procedures - 如何在 where 子句中使用存储函数(过程)?(Entity Framework Core 2.0)
- python - 如何将多个熊猫系列串联成一行?
- rest - 尽管传递了参数,但 REST Api 调用失败,因为缺少参数
- django - 返回 JSON 响应以及可下载的文件
- reactjs - 是否会影响 react-native 应用程序性能的黄色警告?
- asp.net-mvc - mvc 怎样才能取到月份和年份?
- java - 从 URI 下载 zip 文件,并通过多个暂存 URL 进行身份验证和导航
- python - Pandas 列变为 `True` 我们从其他列中选择两个值,当 `False` 选择相同的两个值时
- javascript - Nodejs 后端与 VueJS 前端通信
- c# - 运行“bcdedit /?” 命令行帮助