如何显示数组的平均值?

时间:2020-12-04 21:44:32

I'm making a code that will take the inputs of a teacher so they can calculate the average grade for their class, with a few other things as well.

我正在编写一个代码,它将接受一个老师的输入,这样他们就可以计算他们的班级的平均成绩,还有一些其他的东西。

Unfortunately, my code isn't working properly. I am trying to get it to display the average grade but it keeps generating an error message.

不幸的是,我的代码不能正常工作。我试图让它显示平均成绩,但它不断地产生错误信息。

I need the code to take the class size first. Then take the names of the students and their grades.

我需要代码先取类的大小。然后记下学生的名字和他们的成绩。

Then It should calculate the average grade for the class. This is what I have so far:

然后计算出班级的平均成绩。这是我目前所拥有的:

package program;

import java.util.Scanner;
public class FinalGrades{   
        public static void main(String[]args) {
            Scanner input = new Scanner(System.in);
            System.out.print("How many students are in your class? ");
            int totalStudents = Integer.parseInt(input.nextLine());     
            String[] names = new String[totalStudents];
            double[] scores = new double[totalStudents];
            for(int i = 0; i < totalStudents; i++){
                System.out.print("Name: ");
                names[i] = input.next();
                System.out.print("Score: ");
                scores[i] = input.nextDouble();
            }
            double avg = 0;
            double sum = 0;
            for (int j = 0; j<= totalStudents; j++){
                sum += scores[j];
                avg = sum/totalStudents;
                System.out.println("The average score was: " + avg);    
            }

        }

}       

I would also like the code to display the highest grade with the students name next to it like:

我也希望代码显示最高的成绩,旁边的学生姓名如下:

94.5, Jacob
(but that isn't necessary right now).

94.5, Jacob(但现在没有必要)。

2 个解决方案

#1


3  

Your last for is wrong, you don't calculate avg there, but after for is finished (after you have a sum of all scores). Second, calculate sum as soon as their grades are taken. In order to know student with max grade, as soon the input is given, check for max grade and remember if it is max. Since there can be multiple such students with same best grade, save their position in array.

你的最后一个for是错误的,你不在那里计算avg,但是在for完成之后(在你得到所有分数的总和之后)。第二,在他们的成绩一拿到,就计算总数。为了了解max成绩的学生,一旦输入,检查max成绩并记住是否max。由于可能有多个这样的学生拥有相同的好成绩,所以请保存他们在数组中的位置。

 ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
 ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
 double maxGrade = 0;
 double minGrade = 101;
 double avg = 0;
 double sum = 0;
 for(int i = 0; i < totalStudents; i++){
      System.out.print("Name: ");
      names[i] = input.next();
      System.out.print("Score: ");
      scores[i] = input.nextDouble();
      sum += scores[i];
      System.out.println("Running avg: " + (sum / (i + 1)); // for extra points
      if (scores[i] > maxGrade) {
          bestStudentPosition.clear(); // delete all previous students from list
          maxGrade = scores[i];
          bestStudentPosition.add(new Integer(i));
      } 
      else if (scores[i] == maxGrade) {  // if its same as some student(s) before, add this one to list too
           bestStudentPosition.add(new Integer(i)); 
      } else if (scores[i] < minGrade) {
           worstStudentPosition.clear();
           minGrade = scores[i];
           worstStudentPosition.add(new Integer(i));
      } else if (scores[i] == minGrade) {
           minGrade= scores[i];
           worstStudentPosition.add(new Integer(i));
      }

 }

 avg = sum/totalStudents;   // final avg
 System.out.println("The average score was: " + avg);  
 System.out.println("Best student(s):");
 for (Integer position : bestStudentPosition) { // print all best students
      System.out.println(maxGrade + ", " + names[position]);
 }
 System.out.println("Worst student(s):");
 for (Integer position : worstStudentPosition) { // print all worst students
      System.out.println(minGrade + ", " + names[position]);
 }

#2


1  

Firstly you need to understand average concept. It is sum of all by total.

首先你需要理解平均概念。它是所有的总和。

What you are doing is diving total multiple times.

你所做的就是多次跳水。

double avg = 0;
double sum = 0;
for (int j = 0; j < totalStudents; j++){
   sum += scores[j];
}
avg = sum/totalStudents;
System.out.println("The average score was: " + avg);

If you want a running average then you need to do:

如果你想要跑步的平均水平,那么你需要做:

double avg = 0;
double sum = 0;
for (int j = 0; j < totalStudents; j++){
   sum += scores[j];
   avg += scores[j] / totalStudents;
   System.out.println("The average score was: " + avg);
}

#1


3  

Your last for is wrong, you don't calculate avg there, but after for is finished (after you have a sum of all scores). Second, calculate sum as soon as their grades are taken. In order to know student with max grade, as soon the input is given, check for max grade and remember if it is max. Since there can be multiple such students with same best grade, save their position in array.

你的最后一个for是错误的,你不在那里计算avg,但是在for完成之后(在你得到所有分数的总和之后)。第二,在他们的成绩一拿到,就计算总数。为了了解max成绩的学生,一旦输入,检查max成绩并记住是否max。由于可能有多个这样的学生拥有相同的好成绩,所以请保存他们在数组中的位置。

 ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
 ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
 double maxGrade = 0;
 double minGrade = 101;
 double avg = 0;
 double sum = 0;
 for(int i = 0; i < totalStudents; i++){
      System.out.print("Name: ");
      names[i] = input.next();
      System.out.print("Score: ");
      scores[i] = input.nextDouble();
      sum += scores[i];
      System.out.println("Running avg: " + (sum / (i + 1)); // for extra points
      if (scores[i] > maxGrade) {
          bestStudentPosition.clear(); // delete all previous students from list
          maxGrade = scores[i];
          bestStudentPosition.add(new Integer(i));
      } 
      else if (scores[i] == maxGrade) {  // if its same as some student(s) before, add this one to list too
           bestStudentPosition.add(new Integer(i)); 
      } else if (scores[i] < minGrade) {
           worstStudentPosition.clear();
           minGrade = scores[i];
           worstStudentPosition.add(new Integer(i));
      } else if (scores[i] == minGrade) {
           minGrade= scores[i];
           worstStudentPosition.add(new Integer(i));
      }

 }

 avg = sum/totalStudents;   // final avg
 System.out.println("The average score was: " + avg);  
 System.out.println("Best student(s):");
 for (Integer position : bestStudentPosition) { // print all best students
      System.out.println(maxGrade + ", " + names[position]);
 }
 System.out.println("Worst student(s):");
 for (Integer position : worstStudentPosition) { // print all worst students
      System.out.println(minGrade + ", " + names[position]);
 }

#2


1  

Firstly you need to understand average concept. It is sum of all by total.

首先你需要理解平均概念。它是所有的总和。

What you are doing is diving total multiple times.

你所做的就是多次跳水。

double avg = 0;
double sum = 0;
for (int j = 0; j < totalStudents; j++){
   sum += scores[j];
}
avg = sum/totalStudents;
System.out.println("The average score was: " + avg);

If you want a running average then you need to do:

如果你想要跑步的平均水平,那么你需要做:

double avg = 0;
double sum = 0;
for (int j = 0; j < totalStudents; j++){
   sum += scores[j];
   avg += scores[j] / totalStudents;
   System.out.println("The average score was: " + avg);
}