嘿,我正在尝试根据地图中的数据计算平均值。
首先在代码中有一个 Student 类,其中创建了一个学生,其参数如下: 字符串名字,字符串姓氏,int registerDiaryNumber。
方法 equals、hashCode 和 toString 也被重写。
然后创建了 Grade 类,它以 8 int 作为参数(总共 8 个 Grade)
最后在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: ");
}
}
这就是我陷入困境的地方 - 我不知道如何计算给定成绩的平均值,我尝试将方法放入班级成绩中,但它不起作用。 如果您想检查整个代码,请检查下面的链接(这是 JAVA 训练营的作业,其中指出“使用 HashMap 创建一个程序来存储学生(个人)数据及其成绩。程序必须显示平均值”每个学生的成绩。” 平均值可以是 int 或 double(四舍五入)。
下面是JAVA代码,可能对这个问题有帮助。
这是另一个java文件中的Student类。
package StudentGradeTracker;
public class Student {
private int eng, maths, sci;
public Student(int eng, int maths, int sci) {
this.eng = eng;
this.maths = maths;
this.sci = sci;
}
public Student() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "[" + eng + ", " + maths + ", " + sci + "]";
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMaths() {
return maths;
}
public void setMaths(int maths) {
this.maths = maths;
}
public int getSci() {
return sci;
}
public void setSci(int sci) {
this.sci = sci;
}
}
这是 MainClass,它是另一个 java 文件。
package StudentGradeTracker;
import java.util.HashMap;
public class mainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, Student> hm = new HashMap<>();
hm.put("Hassan", new Student(67, 65, 66));
hm.put("Abrar", new Student(35,45,55));
hm.put("Tausif", new Student(65,35,50));
hm.put("Maikal", new Student(35,25,55));
hm.put("Ali", new Student(64,65,60));
for (String string : hm.keySet()) {
calculateAverage(hm.get(string), hm, string);
}
}
static void calculateAverage(Student student, HashMap<String, Student>
hm1, String key)
{
int sum = 0;
sum = sum + student.getEng() + student.getMaths() + student.getSci();
double avg = (double) sum / hm1.size();
System.out.println("Key : " + key + " Marks : " + hm1.get(key) + " Percentage : " + avg + calculateGrade(avg));
}
static String calculateGrade(double avg){
char result, grade;
if(avg > 55.0 && avg <= 70.0){
result = 'P';
grade = 'A';
}
else if(avg > 45.0 && avg <= 55.0){
result = 'P';
grade = 'B';
}
else if(avg > 33.0 && avg <= 45.0){
result = 'P';
grade = 'C';
}
else{
result = 'F';
grade = ' ';
}
return " Result : " + result + " Grade : " + grade;
}
}
您需要更改
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 循环计算每个学生的平均分数。