首页 > 解决方案 > 在 Java 中使用 HashMap 计算平均(等级)

问题描述

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

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

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

然后创建类 Grade 并以 8 个 int 作为参数(共 8 个等级)

最后主要有这段代码:

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: ");
    }
}

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

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

标签: javahashmapaverage

解决方案


您需要更改以Integers 为例的Grade类的构造函数。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 循环计算每个学生的平均分数。


推荐阅读