【发布时间】:2014-11-27 20:11:13
【问题描述】:
有人能解释为什么程序 1 重复打印一个随机数吗?而下面的程序 2 打印 100 个随机数?另外,是否要编辑程序 1 来执行程序 2 的操作?
程序 1
public class RandomComparison {
public static void main(String[] args){
int rnd = (int) Math.random() * 6 + 1;
for(int i=0; i<100; i++){
System.out.print(rnd);
}
}
}
程序 2
public class RandomPractice {
public static void main (String[] args){
int roll;
String msg = "Here are 100 random rolls of the dice:";
System.out.println(msg);
for (int i=0; i<100; i++){
roll = randomInt(1, 6);
System.out.print(roll);
}
}
public static int randomInt(int low, int high){
int result = (int) (Math.random()*(6) + low);
return result;
}
}
【问题讨论】:
-
因为您正在打印相同的变量
rnd
标签: loops random printing numbers