簡體   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