2016-05-17 294 views
-3

我正在开发一个Java项目,它要求用户输入信息,如姓名,编号,数组中的分数。我需要帮助计算用户输入的平均分数以及如何找出谁的分数最高得分了。这里是我的代码:计算数组中的平均值

package finalproject; 

import java.util.Scanner; 

public class FinalProject { 

    /** 
    * @param args 
    *   the command line arguments 
    */ 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     Cis84[] student = new Cis84[50]; 

     int option; 

     for (int c = 0; c < 50; c++) 
      student[c] = new Cis84(); 

     do { 
      System.out.print(""); 
      System.out.println("1) Add Information"); 
      System.out.println("2) Show report"); 
      System.out.println("3) Exit"); 
      System.out.print("\nEnter option: "); 

      option = input.nextInt(); 

      switch (option) { 
      case 1: 
       String n; 

       double g; 
       int index, 
       i; 

       System.out.println("Which position of the student?"); 
       index = input.nextInt(); 

       System.out.println("What is the student's name:"); 
       n = input.nextLine(); 
       n = input.nextLine(); 

       System.out.println("What is student's Id"); 
       i = input.nextInt(); 

       System.out.println("What is student's score"); 
       g = input.nextDouble(); 

       student[index].setName(n); 
       student[index].setGrade(g); 
       student[index].setId(i); 
       break; 

      case 2: 
       for (int c = 0; c < 50; c++) 
        System.out.println(student[c]); 
       break; 
      case 3: 
       System.out.println("You are done"); 
       break; 
      default: 
       System.out.println("Try again"); 
       break; 
      } 

     } while (option != 3); 

    } 
} 

和类

package finalproject; 

public class Cis84 { 
    private String name; 
    private int id; 
    private double grade; 

    public Cis84() { 
     name = "not input yet"; 
     id = 00000; 
     grade = 0.0; 
    } 

    public Cis84(String n, int i, double g) { 
     name = n; 
     id = i; 
     grade = g; 
    } 

    public void setName(String n) { 
     name = n; 
    } 

    public void setId(int i) { 
     id = i; 
    } 

    public void setGrade(double g) { 
     grade = g; 
    } 

    public String getName() { 
     return name; 
    } 

    public int getId() { 
     return id; 
    } 

    public double getGrade() { 
     return grade; 
    } 

    public String toString() { 
     return String.format("%s\n%d\n%.2f\n", name, id, grade); 
    } 
} 
+0

而问题是什么? – shmosel

+0

显然该如何计算数组的平均值 – sbowde4

回答

0

计算平均会像下面的代码。请记住,平均是所有的值由值

double sum = 0, divisor = 0; 
    for (int k = 0; k < student.length; k++){ 
     sum += student[k].getGrade();//add up all the grades 
     divisor = k;//get the number of total items 
     } 
    return sum/divisor; //divide 
+1

不应该总是student.length ??? – Elltz

+0

是的,但这是一个例子 – sbowde4

2

这的总数除以总和功课,清楚,我觉得不舒服给你一个直接的答案。然而,你想要做的是当显示平均值时,通过整个students数组并总结得分,然后除以数组的大小,可能使用for循环。您可以通过随时增加计数器的大小来随时调整开关盒的选项1。

要找到最高分数,您应该可以使用上面提到的循环的平均计算,并根据以前的最高分数检查分数。记录最高等级的索引并打印出来。

有一些伪代码!

for(size of array which isn't NULL){ 
    add indexed grade to sum; 
    check to see if this index has the highest grade; 
} 

display (sum/size); //the average 
display highest grade; 
+0

我尝试编码来查找最高值,但我总是得到一个错误NullPointerException。谢谢 – vincephng

+0

我的代码:double max; max = student [0] .getGrade();对于(int m = 0; m max) { max = student [m]) { getGrade(); } } – vincephng

+0

这可能是错误的,但不是'if(student [m]!= null'尝试'if(student [m] .getGrade()!= null' –

0
private static double calculateAverage(Cis84[] students) { 

    double sum = 0; 

    for (Cis84 student : students) { 
     sum += student.getGrade(); 
    } 

    return sum/students.length; 
} 

private static double calculateHighestScore(Cis84[] students) { 

    double highestScore = 0; 

    for (Cis84 student : students) { 
     if (student.getGrade() > highestScore) { 
      highestScore = student.getGrade(); 
     } 
    } 

    return highestScore; 
} 

然后,以显示信息:

case 2: 
     System.out.println("Average:"); 
     System.out.println(calculateAverage(student)); 
     System.out.println("Highest score:"); 
     System.out.println(calculateHighestScore(student));