JAVA中如何将Math.random的返回值赋给数组

来源:百度知道 编辑:UC知道 时间:2024/05/10 07:43:31
每次都会出现ArrayIndexOutOfBoundsException~~ 麻烦各位大虾指教
class Random{

public static void main(String[] args) {

int i;
int b[]={};
for( i=0;i<10;i++)
{
b[i]=1+(int)(Math.random()*9+0);
}
System.out.println(b[i]);
}
}
难道这个不能正常打印出结果么?如果不能的话,有没有其他方法实现?

问题在这句上面啊
int b[]={};
你定义一个INT数组用{}给它赋值,你{}里面什么也没写,就是一个空数组,你又要用循环访问它0~9个数组元素当然不行啊;
这样写就可以拉
int b[]=new int[100];

恩,严重支持楼上,既然这样JDK就不装了.机器太垃圾,跑个eclipse都卡的要死.

public class Random{
public static void main(String[] args) throws Exception{

int i;
int b[]={};
try{
for( i=0;i<10;i++)
{
b[i]=1+(int)(Math.random()*9+0);
}
System.out.println(b[i]);
}catch(Exception e){}
}
}

你们在干什么啊???
public class Random {
public static void main(String[] args) {
int[] b = new int[10];
int i;
for (i=0;i<10;i++) {
b[i]=1+(int)(Math.random()*9+0);
System.out.println(b[i]);
}
}
}