java中的骰子模拟

时间:2017-07-24 03:37:48

标签: java simulation dice

我试图模拟滚动模具100次,并打印出1/2/3/4/5/6 i落地的结果。到目前为止,这是我的代码:我尝试使用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次,我该如何修复?

3 个答案:

答案 0 :(得分:3)

在while循环的每次迭代中,您将重新分配count1scount2s和其他值的值。相反,你应该做的是你应该&#34;滚动骰子&#34;然后看看它是什么值,并增加适当的变量。

while (count < 100) {
    int diceRoll = (int) (Math.random() * 6 + 1);
    if (diceRoll == 1) 
        count1s++;
    else if (diceRoll == 2)
        count2s++;
    // ... you get the idea

    count++;
}

作为一个有趣的旁注,使用Java 8有一种非常简单,更酷的方法。

        Stream.generate(() -> (int) (Math.random() * 6 + 1))
             .limit(100L)
             .collect(Collectors.groupingBy(num -> num, 
                 Collectors.counting()))
             .forEach((num, count) -> System.out.println("number of times " + num + " was rolled: " + count));

答案 1 :(得分:1)

每次迭代都要替换以前滚动的数据。您可以将逻辑重写为

// initialization
while(count < 100){
   int currentRoll = (int) (Math.random() * 6 + 1);
   if(currentRoll == 1)
      count1s++;
  // Same logic for all occurances
}

答案 2 :(得分:0)

因为你的逻辑不正确。

while循环内,您为每个卷的外观时间指定了骰子的值。 =&GT;每个人的柜台总是&lt; 7。

此外,每次您的通话Math.random()都会为您提供新值=&gt;应该每次滚动一次。

在这种情况下,使用switch - case语句将是正确的。

  while (count < 100) {
        int num = (int) (Math.random( )*6 + 1);
        switch(num)  {
            case 1: 
                count1s++;
                break;
            case 2:
                count2s++;
                break;
            case 3: 
                count3s++;
                break;
            case 4: 
                count4s++;
                break;
            case 5:
                count5s++;
                break;
            case 6: 
                count6s++;
                break;
        }
        count++;
   }