使用Java Arrays获取最小值和最大值

时间:2021-03-31 22:53:49

thank you for your help.

感谢您的帮助。

Here is the assignment:

这是作业:

Computer Technology Instructor has a small class of 10 students. The instructor evaluates the performance of students in the class by administering 2 midterm tests and a Final Exam. Write a program that prompts the instructor to enter the 10 grades of midterm 1 and store these numbers in an array. Next prompt for the 10 grades of midterm 2 and store these numbers in a different array. Next prompt for the 10 grades of the Final Exam and store these in a different array. Next add midterm1 to midterm2 to Final and store the totals in a different array. Next, scan the array that has the totals and identify the minimum grade and maximum grade. Inform the instructor of the minimum grade and maximum grade.

计算机技术教练有一小班10名学生。教师通过管理2个期中考试和期末考试来评估班级学生的表现。编写一个程序,提示教师输入中期1的10个等级并将这些数字存储在一个数组中。下一个提示中期2的10个等级并将这些数字存储在不同的数组中。下一个提示参加期末考试的10个等级并将其存储在不同的阵列中。接下来将midterm1添加到midterm2到Final,并将总计存储在不同的数组中。接下来,扫描具有总计的阵列,并确定最低等级和最高等级。告知教练最低等级和最高等级。

The two bold phrases are where I am having problems. Everything works except for the minimum grade and maximum grade. Here is what it tells me, after I've only entered numbers between 65 and 100:

两个粗体短语是我遇到问题的地方。一切都有效,除了最低等级和最高等级。在我输入的数字介于65和100之间之后,这就是它告诉我的内容:

The highest test score is: 276 The lowest test score is: 249

最高考试成绩为:276最低考试成绩为:249

Here is my code:

这是我的代码:

import java.util.Scanner;

public class Arrays {
    public static void main(String[] args) {
        // Create a scanner
        Scanner input = new Scanner(System.in);
// Prompt for the 1st mid term
    int [] midTerm1 = new int[10];
    int [] midTerm2 = new int[10];
    int [] finalExam = new int[10];
    int [] grades = new int[10];

    for (int i = 0; i < midTerm1.length; i++){
        System.out.println("Enter the 10 Mid Term 1 grades: ");
        midTerm1[i] = input.nextInt();
    }

    // Prompt for the 2nd mid term


    for (int i = 0; i < midTerm2.length; i++){
        System.out.println("Enter the 10 Mid Term 2 grades: ");
        midTerm2[i] = input.nextInt();
    }


    // Prompt for Final grades


    for (int i = 0; i < finalExam.length; i++){
        System.out.println("Please enter a Final Exam grade: ");
        finalExam[i] = input.nextInt();
    }



    for (int i = 0; i < grades.length; i++){
        grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]);
    }

    int minGrade = grades[0];
    int maxGrade = grades[0];

    for (int i = 0; i < grades.length; i++)

    {
    if (minGrade > grades[i])
      minGrade = grades[i];
    if (maxGrade < grades[i])
      maxGrade = grades[i];

    }


    System.out.print("The highest test score is: " + maxGrade);
    System.out.print("The lowest test score is: " + minGrade);

}
}

1 个解决方案

#1


4  

Edit:

grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]);

Your code may in fact be correct. Since you're adding your three test scores together, expect that the grade will be not between 65 and 100 but rather between 195 and 300.

您的代码实际上可能是正确的。由于您将三个测试分数加在一起,因此期望成绩不会介于65到100之间,而是介于195到300之间。

If you want a number between 65 and 100, this needs to be divided by 3:

如果你想要一个65到100之间的数字,这需要除以3:

grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]) / 3;

or else find some other way to normalize the grades. For instance, if the final is 50% of the grade then you could have:

或者找一些其他方法来标准化成绩。例如,如果决赛是成绩的50%那么你可以:

grades[i] = (25 * midTerm1[i] + 25 * midTerm2[i] + 50 * finalExam[i]) / 100;

But again your current solution may in fact be the correct one.

但是,您目前的解决方案实际上可能是正确的解决方案。

#1


4  

Edit:

grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]);

Your code may in fact be correct. Since you're adding your three test scores together, expect that the grade will be not between 65 and 100 but rather between 195 and 300.

您的代码实际上可能是正确的。由于您将三个测试分数加在一起,因此期望成绩不会介于65到100之间,而是介于195到300之间。

If you want a number between 65 and 100, this needs to be divided by 3:

如果你想要一个65到100之间的数字,这需要除以3:

grades[i] = (midTerm1[i] + midTerm2[i] + finalExam[i]) / 3;

or else find some other way to normalize the grades. For instance, if the final is 50% of the grade then you could have:

或者找一些其他方法来标准化成绩。例如,如果决赛是成绩的50%那么你可以:

grades[i] = (25 * midTerm1[i] + 25 * midTerm2[i] + 50 * finalExam[i]) / 100;

But again your current solution may in fact be the correct one.

但是,您目前的解决方案实际上可能是正确的解决方案。