import java.util.Random;
class random
{
public static void main(String[] args)
{
int n[];
Random rand=new Random();
for(int i=0;i<=10;i++)
{
int n[i]=rand.nextInt();
System.out.println("The random number is::"+n[i]);
}
}
}
这是我在编译时遇到的错误:
我不知道有人可以帮忙吗?
最佳答案
//首先需要声明 n
数组的大小
int n[] = new int [11]; // needs to be 11
Random rand=new Random();
for(int i=0;i<=10;i++)
{
n[i]=rand.nextInt(); // and then just assign
System.out.println("The random number is::"+n[i]);
}
但是说实话,在这段代码中你甚至不需要这个数组,因为它没有被重用
更简单一点
Random rand=new Random();
for(int i=0;i<=10;i++)
{
System.out.println("The random number is::" + rand.nextInt());
}
关于Java在随机声明数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47522906/