如何在我的循环中找到所有测试分数的最高、最低和平均值?
Posted
技术标签:
【中文标题】如何在我的循环中找到所有测试分数的最高、最低和平均值?【英文标题】:How can I find the highest, lowest, and average value of all test scores within my loop? 【发布时间】:2018-08-01 09:12:57 【问题描述】:下午好,或者当您阅读本文时。我试图弄清楚如何找到用户输入的最低、最高和平均测试分数。 我有一个循环来跟踪标记值,在我的例子中是 999。所以当用户输入 999 时,它会退出循环。我还通过检查用户是否输入超过 100 或低于 0 作为输入来进行某种程度的数据验证。但是,我的问题是,如何实现一种方法来获取此代码以找到我的用户输入所需的值。我的代码如下:
import java.util.Scanner;
public class TestScoreStatistics
public static void main(String[] args)
Scanner scn = new Scanner(System.in);
int testScore;
double totalScore = 0;
final int QUIT = 999;
final String PROMPT = "Enter a test score >>> ";
int lowScore;
int highScore;
String scoreString = "";
int counter = 0;
System.out.print(PROMPT);
testScore = scn.nextInt();
while (testScore != QUIT)
if (testScore < 0 || testScore > 100 )
System.out.println("Incorect input field");
else
scoreString += testScore + " ";
counter++;
System.out.print(PROMPT);
testScore = scn.nextInt();
System.out.println(scoreString);
System.out.println(counter + " valid test score(s)");
【问题讨论】:
你可以使用 java-8 的一些特性吗? 试试看ArrayList
如果是这样,那么您可以将分数存储到 List 实现中,然后您可以执行 IntSummaryStatistics summaryStatistics = myList.stream() .collect(Collectors.summarizingInt(Integer::intValue));
然后使用 int max = summaryStatistics.getMax(); int min = summaryStatistics.getMin(); double average = summaryStatistics.getAverage();
收集结果
【参考方案1】:
在保持您的代码几乎相同的同时,您可以这样做:
import java.util.Scanner;
public class TestScoreStatistics
public static void main(String[] args)
Scanner scn = new Scanner(System.in);
int testScore;
double totalScore = 0;
final int QUIT = 999;
final String PROMPT = "Enter a test score >>> ";
int lowScore = 100; //setting the low score to the highest score possible
int highScore = 0; //setting the high score to the lowest score possible
String scoreString = "";
int counter = 0;
System.out.print(PROMPT);
testScore = scn.nextInt();
while (testScore != QUIT)
if (testScore < 0 || testScore > 100 )
System.out.println("Incorect input field");
else
scoreString += testScore + " ";
counter++;
//getting the new lowest score if the testScore is lower than lowScore
if(testScore < lowScore)
lowScore = testScore;
//getting the new highest score if the testScore is higher than highScore
if(testScore > highScore)
highScore = testScore;
totalScore += testScore; //adding up all the scores
System.out.print(PROMPT);
testScore = scn.nextInt();
double averageScore = totalScore / counter; //getting the average
这将检查testScore
是高于还是低于最高和最低分数。该程序还将所有分数相加,然后除以计数器(即有多少次测试)得到平均值。
【讨论】:
这是一个非常优雅而简单的解决方案。 谢谢,只是尽量保持它接近您的代码。【参考方案2】:这就是我会这样做的方式。
// defines your prompt
private static String PROMPT = "Please enter the next number> ";
// validation in a separate method
private static int asInteger(String s)
try
return Integer.parseInt(s);
catch(Exception ex)return -1;
// main method
public static void main(String[] args)
Scanner scn = new Scanner(System.in);
System.out.print(PROMPT);
String line = scn.nextLine();
int N = 0;
double max = 0;
double min = Integer.MAX_VALUE;
double avg = 0;
while (line.length() == 0 || asInteger(line) != -1)
int i = asInteger(line);
max = java.lang.Math.max(max, i);
min = java.lang.Math.min(min, i);
avg += i;
N++;
// new prompt
System.out.print(PROMPT);
line = scn.nextLine();
System.out.println("max : " + max);
System.out.println("min : " + min);
System.out.println("avg : " + avg/N);
验证方法将(在其当前实现中)允许输入任何整数。一旦输入任何无法转换为数字的内容,它将返回 -1,这会触发主循环的中断。
主循环只是跟踪当前的运行总数(以计算平均值),以及到目前为止看到的最大值和最小值。
一旦退出循环,这些值就会简单地打印到System.out
。
【讨论】:
【参考方案3】:只需对您的代码进行最少的更改:
public class Answer
public static void main(String[] args)
Scanner scn = new Scanner(System.in);
int testScore;
final int QUIT = 999;
final String PROMPT = "Enter a test score >>> ";
int maxScore = Integer.MIN_VALUE;
int minScore = Integer.MAX_VALUE;
double totalScore = 0;
double avgScore = 0.0;
int counter = 0;
System.out.print(PROMPT);
testScore = scn.nextInt();
while (testScore != QUIT)
if (testScore < 0 || testScore > 100)
System.out.println("Incorect input field");
else
counter++;
System.out.println("The number of scores you entered is " + counter);
//test for minimum
if(testScore < minScore) minScore = testScore;
System.out.println("Current minimum score = " + minScore);
//test for maximum
if(testScore > maxScore) maxScore = testScore;
System.out.println("Current maximum score = " + maxScore);
//calculate average
totalScore += testScore;
avgScore = totalScore / counter;
System.out.println("Current average score = " + avgScore);
System.out.print(PROMPT);
testScore = scn.nextInt();
【讨论】:
以上是关于如何在我的循环中找到所有测试分数的最高、最低和平均值?的主要内容,如果未能解决你的问题,请参考以下文章
1查询成绩表的总分数,平均分,最低分和最高分。用sql语句怎么写