错误 -> java.lang.ArrayIndexOutOfBoundsException:8

Error -> java.lang.ArrayIndexOutOfBoundsException: 8

我创建了这个多项选择题程序,一切正常,正确答案正在打印出来,但我不断收到:

线程 "main" java.lang.ArrayIndexOutOfBoundsException 中的异常:8 在 MultipleChoices.main(MultipleChoices.java:21)

有人可以告诉我我需要做什么来修复这个错误吗?

        for(int i = 0; i < student[i].length; i++){
            int rightAns = 0;
            for(int j = 0; j < student[i].length; j++){
                if(student[i][j].equalsIgnoreCase(key[j])){
                    rightAns++;
            }
        }

您的第一个 for 循环使用了错误的值。您应该使用 student.length 而不是 student[i].

for(int i = 0; i < student.length; i++){
        int rightAns = 0;
        for(int j = 0; j < student[i].length; j++){
            if(student[i][j].equalsIgnoreCase(key[j])){
                rightAns++;
            }
        }

        System.out.print("Student's " + i + "#correct answer: " + rightAns + "\n");
    }

}