2014-11-23 42 views
0

我目前正在研究一種計算方法,該方法計算一個包含5名學生的課程的平均值。我的問題是如何處理平均法,以便忽略任何尚未創建的學生對象?這裏s1,s2,s3,s4和s5是5個'student'類型的對象。那麼如何編寫公式,以便它忽略任何具有空值的學生,因爲它沒有被添加?更具體地說,我想編輯「其他」部分。我的代碼如下 -如何解決Java中的空對象?

public double average() 
{ 
    if (s1 != null && s2 != null && s3 != null && s4 != null && s5 != null) 
    { 
     avg = (double) (s1.average() + s2.average() + s3.average() + s4.average() + s5.average())/ counter;  
     return avg; 
    } 
    else 
    { 
     avg = 0; 
     return avg; 
    } 
} 
+3

在這種情況下,解決方案很簡單:不要if語句寫這樣。概括。它看起來像你想要一個列表/數組/集合。讓程序爲你做些工作,或多或少。不要手動編碼所有可能的情況。 – keyser 2014-11-23 00:20:37

+0

@keyser是否有任何方法可以在不使用數組的情況下生成此方法,因爲我尚未學習它們。我不介意寫出所有類型的情況,但如果有人可以告訴我,甚至一個... – ScaVenGerS 2014-11-23 00:23:10

+1

是的,但你沒有通過推遲了解他們做任何好處。您可以將x.average()添加到總數中,並記錄沿途的非空數量。 'if(s1!= null)total + = s1.average(); numStudents ++;'那樣你得到5個if語句,每個學生1個。請注意,我不建議您編寫這樣的代碼:p – keyser 2014-11-23 00:25:05

回答

0

首先,您的方法似乎沒有采取任何參數。我認爲它應該。我建議你寫一個variadac function

public static double average(Student... students) { 
    if (students == null) { 
     return 0; 
    } 
    double value = 0; 
    int count = 0; 
    for (Student s : students) { 
     if (s != null) { 
      value += s.average(); 
      count++; 
     } 
    } 
    if (count == 0) { 
     return 0; 
    } 
    return value/count; 
} 

如果你必須使用你的s1 - s5那麼你可以使用loop unrolling

public double average() { 
    double value = 0; 
    int count = 0; 
    if (s1 != null) { 
     value += s1.average(); 
     count++; 
    } 
    if (s2 != null) { 
     value += s2.average(); 
     count++; 
    } 
    if (s3 != null) { 
     value += s3.average(); 
     count++; 
    } 
    if (s4 != null) { 
     value += s4.average(); 
     count++; 
    } 
    if (s5 != null) { 
     value += s5.average(); 
     count++; 
    } 
    if (count == 0) { 
     return 0; 
    } 
    return value/count; 
} 
+0

但是,即使存在1或2個空對象,這是否會使用公式,因爲計數器在技術上會超過0? – ScaVenGerS 2014-11-23 00:44:01

+0

@ScaVenGerS - 您可能想要重新說明該評論。對我來說完全是無稽之談。 – 2014-11-23 00:53:26

+0

@StephenC我在問,假設s4和s5是null,那麼這段代碼會做什麼。它會跳過這些對象,並最終在公式中忽略它們嗎? – ScaVenGerS 2014-11-23 00:55:58

0

我會用一個List<Student>(一List集合包含類型的對象Student),然後將您的方法中的代碼更改爲以下內容:

int sampleSum = 0, sampleQuantity = 0; 
for(Student student : studentList) { 
    if (student == null) { 
     // This student object is not set, so skip to the next object in studentList 
     continue; 
    } 
    sampleSum += student.average; 
    ++sampleQuantity; 
} 
return sampleSum/sampleQuantity; 

請注意,如果studentList中的學生都不爲空,則會引發異常,因爲會發生被零除的情況。如果sampleQuantity在返回語句之前的位置仍然爲零,您可能更願意修改代碼以使其行爲不同。

至於創建List<Student>這是非常容易的:

List<Student> studentList = new ArrayList<>(); 
studentList.add(anyStudentObject); 

,因爲它們是Java的優秀成分這絕對是值得一讀有關the Java Collections Framework

也讀了the for-each loop in Java因爲這是一個非常簡潔的方式遍歷Iterable類型,如List

+1

爲什麼首先會出現空列表中的學生? – EJP 2014-11-23 00:36:51

+0

謝謝你的幫助,我已經開始閱讀數組了,但對於這個作業,我們的教授特意告訴我們不要使用數組,因爲我們在這個項目之前沒有使用數組。雖然我真的很感謝幫助看到另一種創建此方法的方法:D – ScaVenGerS 2014-11-23 00:40:55

+0

@EJP我只是想給用戶一些自由,如果他們想要添加少於5個學生。 – ScaVenGerS 2014-11-23 00:41:36

0

如前所述這是以前更好使用數組或相似,但這裏的反正一個解決方案:

public double average() { 
    double avg = 0; 
    int counter = 0; 

    if (s1 != null) { 
     avg += s1.average(); 
     counter++; 
    } 

    // ... 

    if (s5 != null) { 
     avg += s5.average(); 
     counter++; 
    } 

    if (counter > 0) return avg/counter; 
    else return 0; 
} 
+0

我認爲你的代碼中有一個錯字。我認爲你的final if塊應該包含'avg + = s5.average()'而不是's1'。 – Bobulous 2014-11-23 15:58:27

+0

謝謝,我會編輯它。 – Martomate 2014-11-23 16:04:07