如何在用户输入无效时不使用它?

huangapple go评论101阅读模式
英文:

How to not use a user input if it's invalid?

问题

  1. import java.util.*;
  2. public class QuizScoreStatistics {
  3. public static void main(String args[]) {
  4. float max = 0;
  5. float min = 99;
  6. float avg = 0;
  7. float count = 0;
  8. float sum = 0;
  9. float num;
  10. Scanner scan = new Scanner(System.in);
  11. System.out.println("请输入0到10之间的测试分数。\n输入99结束输入。");
  12. num = scan.nextInt();
  13. float temp = num;
  14. do {
  15. if (num == 99)
  16. System.exit(0);
  17. if (num <= 0 || num > 10)
  18. System.out.println("分数必须在0和10之间。");
  19. if (num >= 0 && num <= 100) {
  20. count++;
  21. sum += num;
  22. if (num > max)
  23. max = num;
  24. if (num < min)
  25. min = num;
  26. }
  27. } while ((num = scan.nextInt()) != 99);
  28. System.out.println("测试统计:");
  29. System.out.println("测试数量:" + count);
  30. System.out.println("最低分:" + min);
  31. System.out.println("最高分:" + max);
  32. System.out.printf("平均分:" + sum / count);
  33. }
  34. }
英文:

My program asks a user for test scores and displays the min/max, # of tests, and the average. However, if the score isn't between 0 and 10 I don't want that number used in my calculations. How would I go about doing that in my do/while loop? I've tried entering
System.exit(0); after line if (num <= 0 || num >10)
System.out.println("Score must be between 10 and 0");
but it breaks my program.
Here is my code:

  1. import java.util.*;
  2. public class QuizScoreStatistics {
  3. public static void main (String args[]) {
  4. float max=0;
  5. float min=99;
  6. float avg=0;
  7. float count=0;
  8. float sum=0;
  9. float num;
  10. Scanner scan=new Scanner(System.in);
  11. System.out.println(&quot;Please input test scores with values between 0-10.\nEnter 99 to finish.&quot;);
  12. num=scan.nextInt();
  13. float temp=num;
  14. do
  15. {
  16. if(num==99)
  17. System.exit(0);
  18. if (num &lt;= 0 || num &gt;10)
  19. System.out.println(&quot;Score must be between 10 and 0&quot;);
  20. if(num&gt;=0 &amp;&amp; num&lt;=100)
  21. {
  22. count++;
  23. sum+=num;
  24. if(num&gt;max )
  25. max=num;
  26. if(num&lt;min)
  27. min=num;
  28. }
  29. } while((num=scan.nextInt())!=99);
  30. // System.out.println(&quot;Test Statistics:&quot;);
  31. System.out.println(&quot;Number of tests: &quot;+count);
  32. System.out.println(&quot;Lowest: &quot;+min);
  33. System.out.println(&quot;Highest: &quot;+max);;
  34. System.out.printf(&quot;Average: &quot; + sum/count);
  35. }

}

答案1

得分: 3

你可以尝试继续执行do-while循环的其余部分。

  1. if (num <= 0 || num > 10){
  2. System.out.println("分数必须在0到10之间");
  3. continue;
  4. }
英文:

You can try continuing the rest of the do-while loop.

  1. if (num &lt;= 0 || num &gt;10){
  2. System.out.println(&quot;Score must be between 10 and 0&quot;);
  3. continue;
  4. }

答案2

得分: 0

做一个继续

如果(num <= 0 || num > 10){
系统。打印("错误答案");
num = scan.nextInt();
继续;
}

继续将跳到循环的下一次迭代。
所以你重新开始循环。

英文:

Do a continue

  1. if(num &lt;= 0 || num &gt; 10){
  2. System.out.println(&quot;wrong asnwer&quot;);
  3. num = scan.nextInt();
  4. continue;
  5. }

continue will skip to the next iteration of the loop.
So you start the loop over.

huangapple
  • 本文由 发表于 2020年4月5日 02:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61033279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定