【发布时间】:2016-08-22 09:40:00
【问题描述】:
对于这段代码,我有一个分数分配给一个分数数组。我想计算成绩(例如:A:3 B:4 C:4 ...)。当我运行程序时,它说:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 70
at ProcessMarks.gradeDistn(ProcessMarks.java:115)
at ProcessMarks.main(ProcessMarks.java:38)
代码:
char[] grades = new char[testMarks.length];
getGrades(testMarks, grades);
int[] counts = gradeDistn (grades);
for (int i = 0; i < counts.length; i++){
System.out.println( grades[i] + " : " + counts[i]);
public static void getGrades (int[] testMarks, char[] grades) {
for (int i = 0; i < testMarks.length; i++) {
if (testMarks[i] >= 90)
grades[i] = 'A';
else if (testMarks[i] >= 75)
grades[i] = 'B';
else if (testMarks[i] >= 60)
grades[i] = 'C';
else if (testMarks[i] >= 50)
grades[i] = 'D';
else if (testMarks[i] >= 45)
grades[i] = 'E';
else
grades[i] = 'F';
}
}
public static int[] gradeDistn (char[] grades){
int[] counts = new int[6];
for (int i = 0; i < grades.length; i++) //count the occurrences
counts[grades[i]]++;
return counts;
}
【问题讨论】:
-
为什么getGrades函数在for循环中?这是您在帖子中输入此内容时的错误吗?