CarterZhang 发表于 2020-12-21 09:53:36

ArrayIndexOutOfBoundsException

package PracticeStudent;

public class PracticeStdent {
        public static void main(String[] args) {
                int[][] student = new int;
                for(int i = 0;i<student.length;i++) {
                        student = new int[]{i+1};
                        student = (int)(Math.random()*5+1);//state:1-5
                        student = (int)(Math.random()*26+75);//score:75-100
                }
}
}

运行后,报错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
        at Class/PracticeStudent.PracticeStdent.main(PracticeStdent.java:9)

求解答!

Sebass 发表于 2020-12-21 10:51:42

07行改为student = new int;就好了
还有08,09行不对应该改为
student = (int)(Math.random()%5+1);//state:1-5
                student = (int)(Math.random()%26+75);//score:75-100
页: [1]
查看完整版本: ArrayIndexOutOfBoundsException