【发布时间】:2018-02-28 15:41:35
【问题描述】:
好的,基本上,我想做的是创建并打印一个包含 100 个随机数的数组。我的尝试似乎失败了......而不是得到 100 个随机数,我似乎得到了从 0 到 99 的数字。有人可以解释我如何解决这个问题吗?非常感谢。
public static void enterRandom()
{
System.out.println("Welcome to the random number generator and arranger!");
System.out.println("Now, watch me generate 100 random numbers, and print them in order...");
System.out.println("...");
System.out.println("...");
System.out.println("...");
Random what = new Random ();
int[] store = new int [100];
for (int i = 0; i <= store.length; i++)
{
store[i] = what.nextInt(100); //not in random?
System.out.println(i);
}
return;
}
}
【问题讨论】:
-
System.out.println(i); - 你正在打印计数器。
-
打印数组而不是计数器 System.out.println(store[i]);
-
1.你打印错了,2.
return是多余的,3.i <=不会有好的结局。 (3) 没有导致异常的事实让我怀疑你没有运行代码,只是为了好玩而提问。 -
@Bathsheba 或者他们这样做了,现在忽略了异常
-
@FedericoklezCulloca:我希望如此。
标签: java arrays for-loop random