【发布时间】:2017-12-29 13:07:48
【问题描述】:
我正在尝试模拟掷骰子 100 次,并打印我降落了多少 1/2/3/4/5/6 的结果。到目前为止,这是我的代码:我正在尝试使用 while 循环进行分配,并且我需要使用 (Math.random( )*6 + 1) 来生成数字。
public class RollingDice {
public static void main(String args[]) {
int count = 0; // number of times die was rolled
int count1s = 0; // number of times 1 was rolled
int count2s = 0; // number of times 2 was rolled
int count3s = 0;
int count4s = 0;
int count5s = 0;
int count6s = 0;
while (count < 100) {
count1s = (int) (Math.random( )*6 + 1);
count2s = (int) (Math.random( )*6 + 1);
count3s = (int) (Math.random( )*6 + 1);
count4s = (int) (Math.random( )*6 + 1);
count5s = (int) (Math.random( )*6 + 1);
count6s = (int) (Math.random( )*6 + 1);
count++;
}
System.out.println("Number of times the die was rolled: "+ count);
System.out.println("Number of times 1 was rolled: " + count1s);
System.out.println("Number of times 2 was rolled: " + count2s);
System.out.println("Number of times 3 was rolled: " + count3s);
System.out.println("Number of times 4 was rolled: " + count4s);
System.out.println("Number of times 5 was rolled: " + count5s);
System.out.println("Number of times 6 was rolled: " + count6s);
}
}
我的代码当前打印:
Number of times the die was rolled: 100
Number of times 1 was rolled: 3
Number of times 2 was rolled: 1
Number of times 3 was rolled: 5
Number of times 4 was rolled: 2
Number of times 5 was rolled: 4
Number of times 6 was rolled: 4
如您所见,它滚动了 100 次,但它只保存了 1 次滚动的结果,而不是 100,我该如何解决这个问题?
【问题讨论】:
标签: java simulation dice