簡體   English   中英

在 Java 中使用 HashMap 計算平均(等級)

[英]Calculate average (grade) using HashMap in Java

嘿,我正在嘗試根據 Map 中的數據計算平均值。

首先在代碼中有一個類Student,其中一個...創建一個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#

例如,您需要更改采用整數ListGrade類的構造函數。

問題的解決方法如下:

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 循環計算每個學生的平均分數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM