繁体   English   中英

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

[英]How can I find the highest, lowest, and average value of all test scores within my loop?

下午好,或者无论何时阅读。 我试图弄清楚如何才能找到用户输入的最低,最高和平均测试分数。 我有一个循环来跟踪前哨值,在我的情况下为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)");

    }

}

在保持代码几乎相同的同时,您可以这样做:

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是高于还是低于最高和最低分数。 该程序还将所有分数相加,然后除以计数器(即有多少个测试)以得到平均值。

这就是我要做的。

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

用最少的代码更改:

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

        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM