【问题标题】:Using an Array to find Minimum, Maximum, and Average in Java在 Java 中使用数组查找最小值、最大值和平均值
【发布时间】:2020-06-17 06:37:54
【问题描述】:

在我们的编程课程介绍中,我们才刚刚开始使用 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

    }
}

【问题讨论】:

  • 您的问题是什么?有什么不工作吗?
  • 我看到的第一个问题是您没有将输入值写入数组。你可以使用testScore = in.nextInt();,但这个testScore 再也不会被使用了。将此值分配给您的数组,例如 listOfScores[index] = testScore 或直接使用 listOfScores[index] = in.nextInt()
  • 对于初学者,您应该将捕获的每个条目存储在数组中 - 而不是 testScore。还有像 Integer.MIN_VALUE/Integer.MAX_VALUE 之类的东西,您应该使用它来播种最大/最小值占位符,以便在每次迭代时进行比较。
  • @JGFMK 我认为最好的方法是用 listOfScores[0] 的值初始化 min 和 max 这样,你的数字范围无关紧要。
  • 这也可以在一个循环中完成

标签: java arrays max average min


【解决方案1】:

您正在使用值 0 初始化索引,然后用 1 覆盖它,因此您正在从第二个值开始访问您的数组。此外,您的循环使用数组的长度作为索引 (15) 结束,这实际上是数组中不存在的第 16 个元素,您肯定会在那里遇到错误。
您需要使用

还有@Nils 所说的首先不保存数组中的值。

【讨论】:

    【解决方案2】:

    实际上你不需要一个数组来完成这个任务。

    我准备了一份待办事项清单来解决你的任务。

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

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 2013-12-23
      • 2016-01-05
      • 2018-09-04
      • 1970-01-01
      • 2021-01-15
      相关资源
      最近更新 更多