【发布时间】:2014-11-04 06:43:46
【问题描述】:
我写了代码,但是没有从double到int的转换。
public class Array {
public static void main(String[] args) {
int i;
int[] ar1 = new int[100];
for(int i = 0; i < ar1.length; i++) {
ar1[i] = int(Math.random() * 100);
System.out.print(ar1[i] + " ");
}
}
}
如何纠正?
【问题讨论】:
-
ar1[i] = ((int) Math.random() * 100);
-
足够智能的 IDE(如 Eclipse 或 IntelliJ)应该能够自行纠正它:
(int) (Math.random() * 100);。 -
不是一个答案,正如你所说你必须使用数学,但类 Random 有一个 nextInt() 函数来创建随机整数
-
你还声明了两次
i,main()中的第一行应该被删除。