2015-10-14 86 views
-2

我需要幫助來創建一個平均方法。我已經完成了一切。它需要計算並返回所有學生測試分數平均值的平均值。我只有返回聲明。儘管如此,我卻遇到了麻煩。這是我的代碼到目前爲止。第一個是課程類,第二個是學生類。我需要幫助來創建一個平均方法

public class Course 
{ 
private String course; 
private Student s1, s2, s3, s4, s5; 
private int studentcount = 0; 

public Course (String name) 
{ 
course = name; 
} 

public Student addStudent(String first, String last, Address home, Address school) 
{ 

if (studentcount == 0){ 
    s1 = new Student(first,last,home,school); 
    studentcount++;   
    return s1; 
}  

if (studentcount == 1) { 
    s2 = new Student(first,last,home,school); 
     studentcount++; 
    return s2; 

} 
else if (studentcount == 2){ 
    s3 = new Student(first,last,home,school); 
    studentcount++; 
     return s3; 

} 
else if (studentcount == 3){ 
    s4 = new Student(first,last,home,school); 
     studentcount++; 
    return s4; 

} 
else if (studentcount == 4) { 
    s5 = new Student(first,last,home,school); 
     studentcount++; 
    return s5; 

} 
else { //the course can only have five students 
    System.out.println("No More students allowed in the class"); 
    return null; 
} 

} 

public double average() //returns the average 
{ 
    return (s1.average() + s1.average() + s1.average() + s1.average() + s1.average())/5.0; 
} 

public String roll() //returns student info for each number of students 
{ 
String results = ""; 

if (studentcount == 1){ 
    results += s1.toString() +"n"; 
    return results; 
}  

if (studentcount == 2) { 
    results += s1.toString() +"n"; 
    results += s2.toString() +"n"; 
    return results; 

} 
else if (studentcount == 3){ 
    results += s1.toString() +"n"; 
    results += s2.toString() +"n"; 
    results += s3.toString() +"n"; 
    return results; 

} 
else if (studentcount == 4){ 
    results += s1.toString() +"n"; 
    results += s2.toString() +"n"; 
    results += s3.toString() +"n"; 
    results += s4.toString() +"n"; 
    return results; 

} 
else if (studentcount == 5) { 
    results += s1.toString() +"n"; 
    results += s2.toString() +"n"; 
    results += s3.toString() +"n"; 
    results += s4.toString() +"n"; 
    results += s5.toString() +"n"; 

    return results; 
    } 
    else{ 
    return null; 
} 

} 
} 


public class Student 
{ 
private String firstName, lastName; 
private Address homeAddress, schoolAddress; 
private int test1, test2, test3; 
//----------------------------------------------------------------- 
// Sets up this Student object with the specified initial values. 
//----------------------------------------------------------------- 
public Student (String first, String last, Address home, Address school) 
{ 
    firstName = first; 
    lastName = last; 
    homeAddress = home; 
    schoolAddress = school; 
    test1 = 0; 
    test2 = 0; 
    test3 = 0; 
} 
//----------------------------------------------------------------- 
// Returns this Student object as a string. 
//----------------------------------------------------------------- 
public String toString() 
{ 
    String result; 
    result = firstName + " " + lastName + "\n"; 
    result += "Home Address:\n" + homeAddress + "\n"; 
    result += "School Address:\n" + schoolAddress; 
    return result; 
} 

public void setTestScore(int t, int g) //sets the test score 
{ 
    if (t == 1) 
    { 
     test1 = g; 
    } 
    else if (t == 2) 
    { 
     test2 = g; 
    } 
    else if (t == 3) 
    { 
     test3 = g; 
    } 
} 

public int getTestScore (int t) //returns the test score 
{ 
    if (t == 1) 
    { 
     return test1; 
    } 
    else if (t == 2) 
    { 
     return test2; 
    } 
    else 
    { 
     return test3; 
    } 
} 
} 
+3

你真的需要學習循環。 –

+0

我不明白爲什麼這會降低選票。我的問題很清楚,我也嘗試了其他程序 – TheUnicornMaster

+0

我不是downvoter,但你的問題太寬泛無法回答。至少這太長時間無法調試。 –

回答

1

我不認爲這樣的練習應該給初學者。它會(如可以在這裏看到的)誘使他們產生令人厭煩和煩人的重複代碼。編程語言已經被開發出來,使得編程變得簡單,而不是脖子上的痛苦。

由於我們在這裏有一個限制問題(無陣列),OTOH可以被視爲一種激勵措施,以制定一個沒有「禁止」功能的策略。但是,我再次懷疑編程入門是否應該在如此早期的階段深入研究這些細節問題。

這就是說,我提出了班級學生的改寫。查看評論。

//禁止使用數組和列表。

public class Course { 
    private String course; 
    private Student s1, s2, s3, s4, s5; 
    private int studentcount = 0; 

    public Course (String name) { 
     course = name; 
    } 

方法傳遞addStudent使用「轉移」已存儲的學生簡單的技巧在s1以騰出空間給下一個學生。儘管一些空值被不必要地複製,但是重複測試並不會更昂貴,並且肯定不太容易出錯。

public Student addStudent(String first, String last, String home, String school){ 
     if(studentcount < 5){ 
      studentcount++; 
      s5 = s4; s4 = s3; s3 = s2; s2 = s1; 
      return s1 = new Student(first,last,home,school); 
     }  
     System.out.println("No more students allowed in the class"); 
     return null; 
    } 

方法卷使用另一個標準技巧來避免重複行。來自s1的數據必須爲所有學生計數顯示更大1,s2必須打印所有計數大於2等。應避免重複擴展字符串,因此應避免使用StringBuilder。

public String roll(){ 
     StringBuilder sb = new StringBuilder(); 
     if (studentcount >= 1) sb.append(s1.toString()).append("\n"); 
     if (studentcount >= 2) sb.append(s2.toString()).append("\n"); 
     if (studentcount >= 3) sb.append(s3.toString()).append("\n"); 
     if (studentcount >= 4) sb.append(s4.toString()).append("\n"); 
     if (studentcount >= 5) sb.append(s5.toString()).append("\n"); 
     return sb.toString(); 
    } 

average方法使用相同的技術,現在累積得分。請注意,轉換爲計算商的兩倍。

public double average(){ 
     int scores = 0; 
     if (studentcount >= 1) 
      scores += s1.getTestScore(1) + s1.getTestScore(2) + s1.getTestScore(3); 
     if (studentcount >= 2) 
      scores += s2.getTestScore(1) + s2.getTestScore(2) + s2.getTestScore(3); 
     if (studentcount >= 3) 
      scores += s3.getTestScore(1) + s3.getTestScore(2) + s3.getTestScore(3); 
     if (studentcount >= 4) 
      scores += s4.getTestScore(1) + s4.getTestScore(2) + s4.getTestScore(3); 
     if (studentcount >= 5) 
      scores += s5.getTestScore(1) + s5.getTestScore(2) + s5.getTestScore(3); 
     return (double)scores/(studentcount*3); 
    } 
} 
+0

非常感謝您的幫助。如果我可以投多個票,我會。然而,我現在在公共學生addStudent中有一個小問題。在這一行中:return s1 = new Student(first,last,home,school);它表示不兼容的類型:java.lang.String不能轉換爲Address。家是突出顯示。 – TheUnicornMaster