我已经问过这个问题,但随后情况有所不同。现在,我在HashMap中获得了一个ArrayList。老实说,我不知道如何在HashMap中为每个学生计算平均值。程序无法在size()和lenght()上运行
我尝试使用for-loop和for-each,但在两种情况下均失败。
我已经问过这个问题,但随后情况有所不同。现在,我在HashMap中获得了一个ArrayList。老实说,我不知道如何在HashMap中为每个学生计算平均值。程序无法在size()和lenght()上运行
我尝试使用for-loop和for-each,但在两种情况下均失败。
import java.util.*;
import java.lang.*;
import java.io.*;
class Student{
String firstName;
String lastName;
public Student(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public boolean equals(Object o){
final Student e = (Student) o;
return this.firstName.equals(e.firstName) && this.lastName.equals(e.lastName);
}
public String toString(){
return firstName + " " + lastName;
}
}
班级成绩{ 私有列表等级=新的ArrayList <>();
/*public Grades(List<Double> grades){
this.grades = grades;
}*/
@Override
public boolean equals(Object o){
final Grades e = (Grades) o;
return this.grades.equals(e.grades);
}
public void addGrade(double grade){
grades.add(grade);
}
public String toString(){
return grades.toString();
}
} class StudentsAndGradesAndAverages {
public static void main (String[] args) throws java.lang.Exception{
Student schoolar1 = new Student("Marcin", "Kossakowski");
Student schoolar2 = new Student("Maria", "Salomea");
Student schoolar3 = new Student("Paulina", "Szymborska");
Student schoolar4 = new Student("Mariusz", "Januszowski");
Grades gradesList1 = new Grades();
gradesList1.addGrade(4);
gradesList1.addGrade(2);
gradesList1.addGrade(3);
gradesList1.addGrade(2);
gradesList1.addGrade(4);
gradesList1.addGrade(5);
Grades gradesList2 = new Grades();
gradesList2.addGrade(5);
gradesList2.addGrade(4);
gradesList2.addGrade(5);
gradesList2.addGrade(5);
gradesList2.addGrade(2);
gradesList2.addGrade(3);
Grades gradesList3 = new Grades();
gradesList3.addGrade(5);
gradesList3.addGrade(6);
gradesList3.addGrade(2);
gradesList3.addGrade(2);
gradesList3.addGrade(6);
gradesList3.addGrade(4);
Grades gradesList4 = new Grades();
gradesList4.addGrade(5);
gradesList4.addGrade(2);
gradesList4.addGrade(3);
gradesList4.addGrade(5);
gradesList4.addGrade(4);
gradesList4.addGrade(5);
HashMap<Student, Grades> studentAndGradesList = new HashMap<Student, Grades>();
studentAndGradesList.put(schoolar1, gradesList1);
studentAndGradesList.put(schoolar2, gradesList2);
studentAndGradesList.put(schoolar3, gradesList3);
studentAndGradesList.put(schoolar4, gradesList4);
System.out.println("Our students grades are: \n ");
for(Map.Entry<Student, Grades> entry: studentAndGradesList.entrySet()){
System.out.println(entry.getKey() + " grades are " + entry.getValue());
}
double sum=0;
}
}
我想计算平均分数,而不管分数多少。