计算成绩不起作用

时间:2016-08-22 09:40:00

标签: java arrays count

对于此代码,我将分数分配给一组分数。我想计算成绩(例如: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;
}

1 个答案:

答案 0 :(得分:0)

问题出在你的gradeDistn - 函数中。 您尝试将等级用作数组的索引。但是等级是一个角色,你需要的是一个数字。 Java只是将您的字符转换为整数,因此转换为字符的ascii代码。

所以这就是你能做的:

jQuery("#shipping-zip-form #country option").each(function()
{
    if (jQuery(this).val()!='US' && jQuery(this).val()!='CA' && jQuery(this).val()!='PR' && jQuery(this).val()!='') {
        jQuery("#shipping-zip-form #country option[value='"+jQuery(this).val()+"']").remove();
    }
});

编辑:

输出应该如下所示:

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] - 'A']++;
        return counts;

    }