简体   繁体   English

使用数组查找 Java 中的最小值、最大值和平均值

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

We are just starting to code in Java in our intro to programming class and I am hung up on where I've gone wrong with this particular assignment.在我们的编程介绍 class 中,我们刚刚开始在 Java 中编写代码,我一直在思考我在这个特定作业中哪里出错了。 The goal is to create a program that enters 15 test scores (values between 1 - 100) that are stored in an array.目标是创建一个程序,输入存储在数组中的 15 个测试分数(值在 1 - 100 之间)。 Then uses that array to compute the output for the minimum, maximum, and average score (average must be an accumulator).然后使用该数组计算 output 的最小值、最大值和平均分数(平均值必须是累加器)。

Infinite loops with break statements are no allowed.不允许使用 break 语句的无限循环。 Below is the code I have started along with notations from the professor.下面是我开始的代码以及教授的注释。

We are running this code in Codiva and when I run it nothing populates.我们在 Codiva 中运行这段代码,当我运行它时,没有任何填充。 Not sure what all I am missing.不知道我错过了什么。

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

    }
}

You are initializing index with value 0, then overwrite it with 1, so you are accessing your array starting with the second value.您正在使用值 0 初始化索引,然后用 1 覆盖它,因此您正在从第二个值开始访问您的数组。 Also your loop ends using the length of the array as an index (15) which is actually the 16th element in your array that doesn't exist, you are definitely getting an error there.此外,您的循环使用数组的长度作为索引 (15) 结束,这实际上是数组中不存在的第 16 个元素,您肯定会在那里遇到错误。
You need to use < instead of <=您需要使用 < 而不是 <=

Also what @Nils said of not saving the values in the array at the first place.还有@Nils 所说的不首先将值保存在数组中。

We are just starting to code in Java in our intro to programming class and I am hung up on where I've gone wrong with this particular assignment.在编程类的入门中,我们才刚刚开始使用Java进行编码,而我对这种特殊分配在哪里出错感到很困惑。 The goal is to create a program that enters 15 test scores (values between 1 - 100) that are stored in an array.目标是创建一个程序,该程序输入存储在数组中的15个测试得分(值介于1-100之间)。 Then uses that array to compute the output for the minimum, maximum, and average score (average must be an accumulator).然后使用该数组计算最小,最大和平均分数的输出(平均值必须是累加器)。

Infinite loops with break statements are no allowed.不允许使用带有break语句的无限循环。 Below is the code I have started along with notations from the professor.下面是我从教授那里写的代码。

We are running this code in Codiva and when I run it nothing populates.我们正在Codiva中运行此代码,当我运行它时,不会填充任何内容。 Not sure what all I am missing.不知道我所缺少的是什么。

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

    }
}

We are just starting to code in Java in our intro to programming class and I am hung up on where I've gone wrong with this particular assignment.在我们的编程课程介绍中,我们才刚刚开始用 Java 编写代码,而我对这个特定作业出错的地方感到困惑。 The goal is to create a program that enters 15 test scores (values between 1 - 100) that are stored in an array.目标是创建一个程序,该程序输入存储在数组中的 15 个测试分数(1 - 100 之间的值)。 Then uses that array to compute the output for the minimum, maximum, and average score (average must be an accumulator).然后使用该数组来计算最小、最大和平均分数的输出(平均值必须是累加器)。

Infinite loops with break statements are no allowed.不允许使用带有 break 语句的无限循环。 Below is the code I have started along with notations from the professor.下面是我开始的代码以及教授的注释。

We are running this code in Codiva and when I run it nothing populates.我们在 Codiva 中运行此代码,当我运行它时没有任何内容。 Not sure what all I am missing.不知道我错过了什么。

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

    }
}

Actually you don't need an array to accomplish this assignment.实际上你不需要一个数组来完成这个任务。

I prepared a to do list to solve your assignment.我准备了一份待办事项清单来解决您的任务。

  1. In the first loop you have to fill your array (if you really need an array), but your code just assign entered value to testScore variable.在第一个循环中,您必须填充数组(如果您确实需要一个数组),但您的代码只是将输入的值分配给testScore变量。

  2. It is a good programming practice that checking user entries.检查用户条目是一个很好的编程习惯。 What if user enters a string value.如果用户输入字符串值会怎样。

  3. You can find min and max values in the first loop.您可以在第一个循环中找到最小值和最大值。 Hint: initialize min = 100;提示:初始化 min = 100; max = 1;最大值 = 1;

  4. To find average, accumulate entered values in a variable like total.要找到平均值,请将输入的值累加到总计等变量中。

  5. Outside of the loop, find average value dividing total with NUM_SCORES .在循环之外,找到总计除以NUM_SCORES的平均值。

In case you need I send my solution:如果您需要,我会发送我的解决方案:

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