簡體   English   中英

使用數組查找 Java 中的最小值、最大值和平均值

[英]Using an Array to find Minimum, Maximum, and Average in Java

在我們的編程介紹 class 中,我們剛剛開始在 Java 中編寫代碼,我一直在思考我在這個特定作業中哪里出錯了。 目標是創建一個程序,輸入存儲在數組中的 15 個測試分數(值在 1 - 100 之間)。 然后使用該數組計算 output 的最小值、最大值和平均分數(平均值必須是累加器)。

不允許使用 break 語句的無限循環。 下面是我開始的代碼以及教授的注釋。

我們在 Codiva 中運行這段代碼,當我運行它時,沒有任何填充。 不知道我錯過了什么。

import java.util.Scanner;

class TestScoresCalulcated {
    public static void main(String[] args) {
        /**Declarations**/
        int index = 0;
        int index2 = 0;
        int min;
        int max;
        int testScore;
        int NUM_SCORES = 15;
        int[] listOfScores = new int[NUM_SCORES];

        Scanner in = new Scanner(System.in);

        for (index = 1; index <= NUM_SCORES; index++) {
            /**TODO:create a loop and make the variable index the loop control variable**/
            System.out.println("Enter in an integer:");
            testScore = in .nextInt();
        }

        min = 1;
        max = 100;

        for (index2 = 1; index2 <= NUM_SCORES; index2++) {
            if (max < listOfScores[index2]) {
                max = listOfScores[index2];
            }
            System.out.println("Doing Max Calculation: " + max);
        }

        for (index2 = 1; index2 <= NUM_SCORES; index2++) {
            if (min > listOfScores[index2]) {
                min = listOfScores[index2];
            }
            System.out.println("Doing Min Calculation: " + min);
        }

        //use the index2 as a loop variable as a index for the array. 
        /*TODO:create another loop
        //TODO:check if the element in the array less than max
          System.out.println("Doing max calulcation");
          //TODO: assign max variable
        //TODO:check if the element in the array less than min
          System.out.println("Doing min calculation");

        //consider doing accumulator calculation here to get the average.

    **/ //end of loop2

        //output the results here

    }
}

您正在使用值 0 初始化索引,然后用 1 覆蓋它,因此您正在從第二個值開始訪問您的數組。 此外,您的循環使用數組的長度作為索引 (15) 結束,這實際上是數組中不存在的第 16 個元素,您肯定會在那里遇到錯誤。
您需要使用 < 而不是 <=

還有@Nils 所說的不首先將值保存在數組中。

在編程類的入門中,我們才剛剛開始使用Java進行編碼,而我對這種特殊分配在哪里出錯感到很困惑。 目標是創建一個程序,該程序輸入存儲在數組中的15個測試得分(值介於1-100之間)。 然后使用該數組計算最小,最大和平均分數的輸出(平均值必須是累加器)。

不允許使用帶有break語句的無限循環。 下面是我從教授那里寫的代碼。

我們正在Codiva中運行此代碼,當我運行它時,不會填充任何內容。 不知道我所缺少的是什么。

import java.util.Scanner;

class TestScoresCalulcated {
    public static void main(String[] args) {
        /**Declarations**/
        int index = 0;
        int index2 = 0;
        int min;
        int max;
        int testScore;
        int NUM_SCORES = 15;
        int[] listOfScores = new int[NUM_SCORES];

        Scanner in = new Scanner(System.in);

        for (index = 1; index <= NUM_SCORES; index++) {
            /**TODO:create a loop and make the variable index the loop control variable**/
            System.out.println("Enter in an integer:");
            testScore = in .nextInt();
        }

        min = 1;
        max = 100;

        for (index2 = 1; index2 <= NUM_SCORES; index2++) {
            if (max < listOfScores[index2]) {
                max = listOfScores[index2];
            }
            System.out.println("Doing Max Calculation: " + max);
        }

        for (index2 = 1; index2 <= NUM_SCORES; index2++) {
            if (min > listOfScores[index2]) {
                min = listOfScores[index2];
            }
            System.out.println("Doing Min Calculation: " + min);
        }

        //use the index2 as a loop variable as a index for the array. 
        /*TODO:create another loop
        //TODO:check if the element in the array less than max
          System.out.println("Doing max calulcation");
          //TODO: assign max variable
        //TODO:check if the element in the array less than min
          System.out.println("Doing min calculation");

        //consider doing accumulator calculation here to get the average.

    **/ //end of loop2

        //output the results here

    }
}

在我們的編程課程介紹中,我們才剛剛開始用 Java 編寫代碼,而我對這個特定作業出錯的地方感到困惑。 目標是創建一個程序,該程序輸入存儲在數組中的 15 個測試分數(1 - 100 之間的值)。 然后使用該數組來計算最小、最大和平均分數的輸出(平均值必須是累加器)。

不允許使用帶有 break 語句的無限循環。 下面是我開始的代碼以及教授的注釋。

我們在 Codiva 中運行此代碼,當我運行它時沒有任何內容。 不知道我錯過了什么。

import java.util.Scanner;

class TestScoresCalulcated {
    public static void main(String[] args) {
        /**Declarations**/
        int index = 0;
        int index2 = 0;
        int min;
        int max;
        int testScore;
        int NUM_SCORES = 15;
        int[] listOfScores = new int[NUM_SCORES];

        Scanner in = new Scanner(System.in);

        for (index = 1; index <= NUM_SCORES; index++) {
            /**TODO:create a loop and make the variable index the loop control variable**/
            System.out.println("Enter in an integer:");
            testScore = in .nextInt();
        }

        min = 1;
        max = 100;

        for (index2 = 1; index2 <= NUM_SCORES; index2++) {
            if (max < listOfScores[index2]) {
                max = listOfScores[index2];
            }
            System.out.println("Doing Max Calculation: " + max);
        }

        for (index2 = 1; index2 <= NUM_SCORES; index2++) {
            if (min > listOfScores[index2]) {
                min = listOfScores[index2];
            }
            System.out.println("Doing Min Calculation: " + min);
        }

        //use the index2 as a loop variable as a index for the array. 
        /*TODO:create another loop
        //TODO:check if the element in the array less than max
          System.out.println("Doing max calulcation");
          //TODO: assign max variable
        //TODO:check if the element in the array less than min
          System.out.println("Doing min calculation");

        //consider doing accumulator calculation here to get the average.

    **/ //end of loop2

        //output the results here

    }
}

實際上你不需要一個數組來完成這個任務。

我准備了一份待辦事項清單來解決您的任務。

  1. 在第一個循環中,您必須填充數組(如果您確實需要一個數組),但您的代碼只是將輸入的值分配給testScore變量。

  2. 檢查用戶條目是一個很好的編程習慣。 如果用戶輸入字符串值會怎樣。

  3. 您可以在第一個循環中找到最小值和最大值。 提示:初始化 min = 100; 最大值 = 1;

  4. 要找到平均值,請將輸入的值累加到總計等變量中。

  5. 在循環之外,找到總計除以NUM_SCORES的平均值。

如果您需要,我會發送我的解決方案:

import java.util.InputMismatchException;
import java.util.Scanner;

class TestScoresCalulcated {
    public static void main(String[] args) {
        /** Declarations **/
        int min = 100;
        int max = 1;
        int total = 0;
        int testScore;
        int NUM_SCORES = 15;


        Scanner in = new Scanner(System.in);

        for (int i = 0; i < NUM_SCORES;) {
            try {
                System.out.println("Enter " + (i + 1) + ". value: ");
                testScore = in.nextInt();

                if (testScore < 1 || testScore > 100) {
                    throw new IllegalArgumentException();
                }

                if (testScore < min) {
                    min = testScore;
                }

                if (testScore > max) {
                    max = testScore;
                }

                total += testScore;
                ++i;

            } catch (InputMismatchException | IllegalArgumentException e) {
                System.out.println("Please enter numbers between 1 and 100\n");
                in.nextLine();
            }

        }

        System.out.println("\nOutput:");
        System.out.println("Min: " + min);
        System.out.println("Max: " + max);
        System.out.println("Average: " + total / NUM_SCORES);

        in.close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM