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

huangapple go评论90阅读模式
英文:

Calculate average (grade) using HashMap in Java

问题

嗨,我正在尝试从地图中的数据计算平均值。

首先,在代码中有一个名为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: ");
    }
}

这就是我卡住的地方 - 我不知道如何从给定的成绩中计算平均值。我尝试将方法放在Grade类中,但没有成功。如果您想检查整个代码,请查看下面的链接(这是来自JAVA训练营的一个任务,其中写着“使用HashMap创建一个程序,将学生(个人)数据和他们的成绩存储起来。程序必须显示每个学生的平均成绩。”平均成绩可以是int或double(四舍五入)。

链接

英文:

Hey there I'm trying to calculate average from data in Map.

First in a code there is class Student in which a... well a student is created with parameters as follow:
String firstName, String lastName, int registerDiaryNumber.

Also methods equals, hashCode and toString are Overridden.

Then the class Grade is created and it takes 8 int as parameter (8 grades total)

Finally in main there is this piece of code:

public static void main (String[] args) {
Student student1 = new Student(&quot;Terry&quot;, &quot;Pratchett&quot;, 2);
Student student2 = new Student(&quot;Arashi&quot;, &quot;Ryuuno&quot;, 3);
Student student3 = new Student(&quot;Nobunaga&quot;, &quot;Oda&quot;, 4); 
Student student4 = new Student(&quot;Pheonix&quot;, &quot;Wright&quot;, 5);
Student student5 = new Student(&quot;Ainz&quot;, &quot;Gown&quot;, 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&lt;Student, Grades&gt; mathGrades = new HashMap&lt;&gt;();
mathGrades.put(student1, student1Math);
mathGrades.put(student2, student2Math);
mathGrades.put(student3, student3Math);
mathGrades.put(student4, student4Math);
mathGrades.put(student5, student5Math);
for (Map.Entry&lt;Student, Grades&gt; entry : mathGrades.entrySet()){
System.out.println(&quot;Number &quot; + entry.getKey() +&quot; got grades as follow:&quot; +entry.getValue());
System.out.println(&quot;Student average grade is: &quot;);
}
}

And thats where I stuck - I have no idea how to calculate average from given grades I tried putting methods in class grade but it did not work.
If you want to check the whole code then please check the link below (This is an assignment from JAVA bootcamp and it states "using HashMap create a program that will store Students (personal) data and their grades. Program has to show average grade of every student."
The average can be either an int or double (rounded).

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

答案1

得分: -1

你需要更改 Grade 类的构造函数,该构造函数接受一个整数列表作为示例。

问题的解决方案将会非常简单,如下所示:

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(new ArrayList<Integer>() {{
        add(5);
        add(6);
        add(5); // 在此添加更多...
    }});
    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 (Double currentGrade : studentGrades) {
            currentStudentAvgSum += currentGrade;
        }
        double currentStudentAvg = currentStudentAvgSum / studentGrades.size();

        System.out.println("学生平均成绩为:" + currentStudentAvg);
    }
}

此外,for 循环计算每个学生的平均分。

英文:

You need to change the constructor of Grade class that takes a List of Integers for example.

The solution of the problem will be as simple as following:

public static void main(String[] args) {
Student student1 = new Student(&quot;Terry&quot;, &quot;Pratchett&quot;, 2);
Student student2 = new Student(&quot;Arashi&quot;, &quot;Ryuuno&quot;, 3);
Student student3 = new Student(&quot;Nobunaga&quot;, &quot;Oda&quot;, 4);
Student student4 = new Student(&quot;Pheonix&quot;, &quot;Wright&quot;, 5);
Student student5 = new Student(&quot;Ainz&quot;, &quot;Gown&quot;, 1);
// A static initialization of a list of integers
Grades student1Math = new Grades(new ArrayList&lt;Integer&gt;() {{
add(5);
add(6);
add(5); // add more here...
}});
Grades student2Math = new Grades(new ArrayList&lt;Integer&gt;() {{
add(5);
add(6);
add(5);
}});
Grades student3Math = new Grades(new ArrayList&lt;Integer&gt;() {{
add(3);
add(3);
add(5);
}});
Grades student4Math = new Grades(new ArrayList&lt;Integer&gt;() {{
add(5);
add(4);
add(5);
}});
Grades student5Math = new Grades(new ArrayList&lt;Integer&gt;() {{
add(4);
add(5);
add(5);
}};
Map&lt;Student, Grades&gt; mathGrades = new HashMap&lt;&gt;();
mathGrades.put(student1, student1Math);
mathGrades.put(student2, student2Math);
mathGrades.put(student3, student3Math);
mathGrades.put(student4, student4Math);
mathGrades.put(student5, student5Math);
for (Map.Entry&lt;Student, Grades&gt; entry : mathGrades.entrySet()){
double currentStudentAvgSum = 0.0;
List&lt;Integer&gt; studentGrades = mathGrades.get(entry.getKey());
for(Dobule currentGrade : studentGrades){
currentStudentAvgSum + = currentGrade;
}
double currentStudentAvg = currentStudentAvgSum / studentGrades.size();
System.out.println(&quot;Student average grade is: &quot; + currentStudentAvg);
}
}

And the for loop calculates average scores per student.

huangapple
  • 本文由 发表于 2020年9月15日 05:15:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63891939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定