2015-04-19 43 views
0

這是實驗室的一部分,我找不出來......我無法弄清楚名單類中的addGrade方法有什麼問題,我必須給學生添加一個成績,如果學生尚不存在,創建一個新學生,然後添加成績。請注意,最初,這個類沒有實例變量Student stu,我試圖讓事情起作用時添加了它。爲什麼我無法將元素添加到Java中的LinkedList中?

學生提供了一個構造函數,學生成績保存在一個鏈表中。我只把代碼的一部分放在這裏......它有一些方法來獲得學生的名字,獲得分數,增加分數,並獲得平均分數。

我的代碼在經過一些編輯後不再有效......當它部分工作時,它只是覆蓋了以前的最新版本的學生。學生a添加了5年級,然後學生b添加了7,然後學生a再次添加了10.這應該讓學生a在鏈接列表中有2個條目(5,10)。當我運行我的代碼時,它只有10個學生,但也沒有完全工作。

public class Student { 
private String name; 
private List scores = new LinkedList<>(); 

    public Student(String name) 
    { 
    this.name = name; 
    } 

    public void addGrade(int score) 
    { 
    scores.add(score); 
    } 


public class Roster { 

String name; 
int score; 
Student stu; 

    //Adds a grade to the end of a list of grades for the named student. 
    //Should work even if no student with this name has ever been seen before. 
    public void addGrade(String name, int score) { 
     Student temp = new Student(name); 
     stu.addGrade(score); 
    } 
    //Gets the specified grade from the named student's scores. 
    public int getGrade(String name, int index) { 
     int a = stu.getScore(index); 
     return a; 
    } 
    //Gets the average for the named student. 
    public double getAverage(String name) { 
     return stu.getAverage(); 
    } 
} 
+0

現在,讓我們開始 「工作」 的定義。代碼的工作原理是什麼?什麼部分特別是造成你的麻煩? – Makoto

+1

你可能想在'Roster'中(或者是一個人的類:p)想要一個'Student'的List。請注意,使用學生姓名作爲鍵和學生作爲價值的「地圖」在這裏可能非常方便(更容易檢查現有學生)。 –

+1

你可能想要一個'Map 分數而不是鏈接列表(將課程映射到分數)。 – alfasin

回答

1

名冊是學生名單。

學生有一個分數列表。

這不是所有的代碼,你需要,只是你的名冊類的一部分和addGrade()方法存在的:

public class Roster { 
    List<Students> students = new LinkedList<Student>(); 

    public void addGrade(String name, int score) { 
     // Student s = null; 
     // Search for existing student. 
     for (Student currentStu : students) { 
      if (currentStu.name.equals(name) { 
       s = currentStu; 
      } 
     } 

     if (s == null) { 
      //Student not in our roster. Add him. 
      s = new Student(name); 
     } 

     // Add the score to that student. 
     s.addGrade(score); 
    } 
} 
0

有很多的事情,此代碼怎麼回事,但我給你對可能出錯的一些提示。我強烈建議你接觸你的老師,導師或者TA來關閉一些事情的循環,但是在大多數情況下,這裏是我所看到的一些概念錯誤。

  • 學生是最基本的信息。名冊包含一系列學生。很有可能您希望在您的名冊類中使用List<Student>。你不需要任何比名冊中的其他領域。

    你應該提供一種方法給特定的學生添加更多的分數,但是這個特殊的方面我會留給你和老師討論。

  • 您當前的鏈接列表聲明的分數是無類型的。由於您在編譯時會生成未經檢查的警告,因此您會對此產生不滿,如果您不小心將一個非數字添加到該鏈接列表中,則在運行時嘗試對其執行數學運算時會出現錯誤。

    同樣有趣的是,您使用鏈接列表而不是數組支持列表,因爲您打算將其索引到列表中。出於性能原因,我會建議使用ArrayList

    說了這麼多,你會想輸入它作爲List<Integer> scores = new ArrayList<>()

  • 您需要一種按名稱搜索學生的方法。這是有點棘手,因爲你在那裏存儲Student條目,但它可以完成。我將描述一個非常基本的方法,但我希望你從它那裏拿到它:

    • 迭代你在名單中的所有學生的集合。
    • 如果學生的姓名與當前實例匹配,那麼你找到了你的學生。
    • 如果列表中沒有包含名稱的學生,則必須返回一個表示該處沒有學生的數字。 0對於平均值不好,因爲你可以的平均得分爲0;也許-1會工作。
+0

如果找不到學生,則返回'0'或'-1'是反模式。這是Java,而不是C.使用Map。稱過度優化很奇怪。 –

+0

我個人不想這樣做,但我不相信這個學生已經進展到足以理解拋出異常的含義。我真的非常喜歡他們的老師,而不是我們,因爲我們或多或少熟悉良好實踐和多年經驗,但因爲我沒有提供具體的代碼(和只有建議),我並不太在意這個建議。 – Makoto

+0

也許,也許,他們會閱讀一個例外是什麼,並學習一些東西。我想給他們這個機會。感謝downvote ... –

0

一個Map<String,Student>這裏的關鍵是:

public class Roster { 
    private final Map<String, Student> students = new HashMap<>(); 

    //Adds a grade to the end of a list of grades for the named student. 
    //Should work even if no student with this name has ever been seen before. 
    public void addGrade(String name, int score) { 
     Student stu = students.get(name); 
     if (stu == null) { 
      stu = new Student(name); 
      students.put(name, stu); 
     } 
     stu.addGrade(score); 
    } 

    //Gets the specified grade from the named student's scores. 
    public int getGrade(String name, int index) { 
     Student student = students.get(name); 
     if (student == null) { 
      throw new IllegalStateException("Student not found: " + name); 
     } 
     return student.getScore(index); 
    } 

    //Gets the average for the named student. 
    public double getAverage(String name) { 
     Student student = students.get(name); 
     if (student == null) { 
      throw new IllegalStateException("Student not found: " + name); 
     } 
     return student.getAverage(); 
    } 
} 
+0

地圖以學生姓名的形式存儲冗餘信息。這是一個很好的優化,但不是完全需要這個特定的練習。 – Makoto

+0

您如何知道練習的要求? –

+0

我不知道。但是我可以告訴你,如果需要一個Map,將使用一個Map來代替List,因爲這就是這些練習的工作原理。 – Makoto

相關問題