如何在循环中找到所有测试分数的最高,最低和平均值?

时间:2018-02-21 17:17:54

标签: java loops max average min

下午好,或者每当你读这篇文章的时候。我试图找出如何找到用户输入的最低,最高和平均测试分数。 我有一个循环跟踪一个标记值,在我的情况下是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)");

    }

}

3 个答案:

答案 0 :(得分: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是高于还是低于最高和最低分数。该程序还将所有分数加在一起,并将其除以计数器(这是有多少次测试)以获得平均值。

答案 1 :(得分:0)

我就是这样做的。

// 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

答案 2 :(得分:0)

只需对代码进行最少的更改:

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();

        }
    }
}