2015-04-19 41 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

相关问题