我很難解決我的問題。我必須按最高平均值mark
降序排列students
,並顯示所有學生的平均mark
。我將包括我的Main
課程和來自其他課程的聲明。從ArrayList裏面排列哈希映射的值
public Main()
{
ArrayList<Student> students = new ArrayList<Student>();
Student A = new Student("John", "Doe", 1000);
students.add(A);
Student B = new Student("Michael", "Hawk", 2000);
students.add(B);
Student C = new Student("Nicholas", "Johnson", 3000);
students.add(C);
HashMap<Integer, ArrayList<PassedExams>> map = new HashMap<Integer, ArrayList<PassedExams>>();
for (Student student : students)
{
map.put(student.getIndeks(), new ArrayList<PassedExams>());
}
for (Entry<Integer,ArrayList<PassedExams>> exam : map.entrySet())
{
if (exam.getKey() == 1000)
{
ArrayList<PassedExams> passedExam = exam.getValue();
passedExam.add(new PassedExams("CS102", 6));
passedExam.add(new PassedExams("CS220", 8));
exam.setValue(passedExam);
}
if (exam.getKey() == 2000)
{
ArrayList<PassedExams> passedExam = exam.getValue();
passedExam.add(new PassedExams("MA101", 10));
passedExam.add(new PassedExams("CS101", 7));
exam.setValue(passedExam);
}
if (exam.getKey() == 3000)
{
ArrayList<PassedExams> passedExam = exam.getValue();
passedExam.add(new PassedExams("CS115", 9));
passedExam.add(new PassedExams("MA102", 7));
exam.setValue(passedExam);
}
}
for (Student student : students)
{
System.out.println(student.toString() + " " + map.get(student.getIndex()));
}
}
`
public class PassedExams
{
private String code;
private Integer mark;
// get and set methods
// aswell as toString();
}
`
public class Student
{
private String name, surname;
private Integer index;
// get and set methods
// aswell as toString();
}
編輯:添加輸入和輸出
什麼是默認看出:
Student Name: John Surname: Doe Index: 1000 [Predmet: Code: CS102 Mark: 6, Predmet: Code: CS220 Mark: 8]
Student Name: Michael Surname: Hawk Index: 2000 [Predmet: Code: MA101 Mark: 10, Predmet: Code: CS101 Mark: 7]
Student Name: Nicholas Surname: Johnson Index: 3000 [Predmet: Code: CS115 Mark: 9, Predmet: Code: MA102 Mark: 7]
應該怎樣排序後看到:
Student Name: Michael Surname: Hawk Index: 2000 [Predmet: Code: MA101 Mark: 10, Predmet: Code: CS101 Mark: 7]
Student Name: Nicholas Surname: Johnson Index: 3000 [Predmet: Code: CS115 Mark: 9, Predmet: Code: MA102 Mark: 7]
Student Name: John Surname: Doe Index: 1000 [Predmet: Code: CS102 Mark: 6, Predmet: Code: CS220 Mark: 8]
,總平均mark
:
Total average mark from students is: 7.83
此外,我想我應該改變從Integer
到Double
因爲線以上。
請包括使用此代碼的示例輸入和輸出。 – ChiefTwoPencils
@ChiefTwoPencils我剛剛做到了。 – brajevicm
難道你不應該在學生身上有一個名爲'List passedExams;'的房產,這樣你就可以在學生和考試之間建立直接聯繫,而不是使用'int'來連接他們嗎? –
Draken