在 Java 中使用 HashMap 计算平均值(成绩)

Calculate average (grade) using HashMap in Java

嘿,我正在尝试根据 Map 中的数据计算平均值。

首先在代码中有 class 学生,其中一个......以及一个学生是使用以下参数创建的: String firstName, String lastName, int registerDiaryNumber.

也覆盖了方法 equals、hashCode 和 toString。

然后创建 class 成绩,它需要 8 个整数作为参数(总共 8 个成绩)

最后在main中有这段代码:

public static void main (String[] args) {

Student student1 = new Student("Terry", "Pratchett", 2);
Student student2 = new Student("Arashi", "Ryuuno", 3);
Student student3 = new Student("Nobunaga", "Oda", 4); 
Student student4 = new Student("Pheonix", "Wright", 5);
Student student5 = new Student("Ainz", "Gown", 1);

Grades student1Math = new Grades(4, 5, 4, 3, 2, 5, 5, 3);
Grades student2Math = new Grades(5, 6, 5, 5, 6, 5, 5, 4);
Grades student3Math = new Grades(3, 3, 5, 4, 3, 4, 5, 2);
Grades student4Math = new Grades(5, 4, 5, 3, 4, 5, 3, 5);
Grades student5Math = new Grades(4, 5, 5, 3, 3, 4, 6, 5);

Map<Student, Grades> mathGrades = new HashMap<>();
mathGrades.put(student1, student1Math);
mathGrades.put(student2, student2Math);
mathGrades.put(student3, student3Math);
mathGrades.put(student4, student4Math);
mathGrades.put(student5, student5Math);

for (Map.Entry<Student, Grades> entry : mathGrades.entrySet()){
    System.out.println("Number " + entry.getKey() +" got grades as follow:" +entry.getValue());
    System.out.println("Student average grade is: ");
    }
}

这就是我卡住的地方 - 我不知道如何根据给定的成绩计算平均值我尝试将方法放在 class 等级中,但它没有用。 如果您想检查整个代码,请检查下面的 link(这是来自 JAVA 训练营的作业,它声明“使用 HashMap 创建一个程序来存储学生(个人)数据及其成绩. 程序 必须 显示每个学生的平均成绩。” 平均值可以是 int 或 double(四舍五入)。

https://kodilla.com/pl/project-java/173235#

您需要更改 Grade class 的构造函数,例如采用 List 整数。

问题的解决方法如下:

public static void main(String[] args) {
    Student student1 = new Student("Terry", "Pratchett", 2);
    Student student2 = new Student("Arashi", "Ryuuno", 3);
    Student student3 = new Student("Nobunaga", "Oda", 4);
    Student student4 = new Student("Pheonix", "Wright", 5);
    Student student5 = new Student("Ainz", "Gown", 1);

    // A static initialization of a list of integers
    Grades student1Math = new Grades(new ArrayList<Integer>() {{
        add(5);
        add(6);
        add(5); // add more here...
    }});
    Grades student2Math = new Grades(new ArrayList<Integer>() {{
        add(5);
        add(6);
        add(5);
    }});
    Grades student3Math = new Grades(new ArrayList<Integer>() {{
        add(3);
        add(3);
        add(5);
    }});
    Grades student4Math = new Grades(new ArrayList<Integer>() {{
        add(5);
        add(4);
        add(5);
    }});
    Grades student5Math = new Grades(new ArrayList<Integer>() {{
        add(4);
        add(5);
        add(5);
    }};

    Map<Student, Grades> mathGrades = new HashMap<>();
    mathGrades.put(student1, student1Math);
    mathGrades.put(student2, student2Math);
    mathGrades.put(student3, student3Math);
    mathGrades.put(student4, student4Math);
    mathGrades.put(student5, student5Math);

    for (Map.Entry<Student, Grades> entry : mathGrades.entrySet()){
        double currentStudentAvgSum = 0.0;
        
        List<Integer> studentGrades = mathGrades.get(entry.getKey());
        for(Dobule currentGrade : studentGrades){
            currentStudentAvgSum + = currentGrade;
        }
        double currentStudentAvg = currentStudentAvgSum / studentGrades.size();
        
        System.out.println("Student average grade is: " + currentStudentAvg);
    }
}

然后 for 循环计算每个学生的平均分数。