掷骰子模拟程序 Java
发布网友
发布时间:2022-04-24 11:58
我来回答
共1个回答
热心网友
时间:2023-10-12 00:31
public class TestDieRoll {
private class DieRoll {
private final int DIES = 6;
private int roll() {
return (int) (Math.floor(Math.random() * DIES + 1));
}
}
public static void main(String[] args) {
DieRoll d = new TestDieRoll().new DieRoll();
int[] arr = new int[6];
for (int i = 0; i < 1000; i++)
arr[d.roll()-1]++;
for (int i = 0; i < arr.length; i++) {
System.out.println("掷得"+(i+1)+"点的次数为 "+(arr[i])+"次");
}
}
}