我有一个包含表示中期成绩的值的双精度数组,我需要创建一个具有相同值的char数组,但需要它来显示字母成绩。例如,我需要从{89.6,73.9,96.9}转到{B,C,A}。到目前为止,这是我的代码:
int numStudents = 60;
int studNum = 1;
double[] midtermGrades = new double[numStudents];
Scanner grades = new Scanner(System.in);
System.out.println("Here are the scores for the most recent test taken by your class:");
for (int i = 0; i < midtermGrades.length; i++) {
midtermGrades[i] = (60 + (double)(Math.random() * 40));
System.out.printf("Student " + studNum++ + " : " + "%.1f\n", midtermGrades[i]);
}
double total = 0;
for (int i = 0; i < midtermGrades.length; i++) {
total += midtermGrades[i];
}
double studAvg = (total / midtermGrades.length);
System.out.printf("The average grade for your class was " + "%.1f\n", studAvg);
int aboveAvg = 0;
for (int i = 0; i < midtermGrades.length; i++) {
if (midtermGrades[i] > studAvg) {
aboveAvg++;
}
}
System.out.println("There are " + aboveAvg + " students in your class who scored above the average.\n");
char[] letterGrades = new char[midtermGrades.length];
for (int i = 0; i < midtermGrades.length; i++) {
letterGrades[i] = (char)midtermGrades[i];
}
}
您可以尝试使用其他字符数组并指定评分标准
char midtermgrades2 = new char[numStudents] ;
for(i=0;i<midtermgrades.length;i++)
{If(midtermsgrades[i]<100 && midtermsgrades[i]>80)
Midtermgrades2[i]='A';
else
//specify more conditions like this
}