Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Lottal.getRandom(Lottal.java:21)
at Lottal.main(Lottal.java:34)
------------------------------------------------
import java.lang.reflect.Array;
import java.util.Random;
public class Lottal {
private int num;
private int length;
private int[] array=new int[length];//length是在构造方法之后才能赋值
public Lottal(int num, int length) {
this.num = num;
this.length = length;
}
public void getRandom(){
Random rnd = new Random();
for(int i=0;i<length;i++){
System.out.print(" "+rnd.nextInt(num+1));
array[i]=rnd.nextInt(num+1);
}
}
public static void main(String[] args) {
Lottal lottal = new Lottal(3,3);
lottal.getRandom();
}
}
----------------------------------------------------------
改完之后
import java.lang.reflect.Array;
import java.util.Random;
public class Lottal {
private int num;
private int length;
private int[] array;
public Lottal(int num, int length) {
this.num = num;
this.length = length;
}
public void getRandom(){
Random rnd = new Random();
array=new int[length];
for(int i=0;i<length;i++){
System.out.print(" "+rnd.nextInt(num+1));
array[i]=rnd.nextInt(num+1);
}
}
public static void main(String[] args) {
Lottal lottal = new Lottal(93,3);
lottal.getRandom();
}
}
1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
原创
©著作权归作者所有:来自51CTO博客作者mb6444ed45406a4的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:eclipse 插件导入
下一篇:Lottal输入输出小程序
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java Thread Dump文件分析
Java Thread Dump文件分析
java 堆栈 Java