2016-05-17 224 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));