【问题标题】:Dice simulation in javajava中的骰子模拟
【发布时间】: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


    【解决方案1】:

    在您的 while 循环的每次迭代中,您都在重新分配 count1scount2s 和其他的值。相反,您应该做的是“掷骰子”,然后查看它的值,并增加适当的变量。

    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));
    

    【讨论】:

    • 非常感谢!!快把我逼疯了。很简单的人
    • Java 8 上不错的解决方案。谢谢。
    【解决方案2】:

    因为你的逻辑不正确。

    while 循环中,您为每个掷骰的出现时间分配了骰子的值。 => 每个计数器总是

    此外,每次调用 Math.random() 都会给你新的价值 => 每次滚动都应该调用一次。

    在这种情况下,使用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++;
       }
    

    【讨论】:

    • 谢谢,忘了switch语句
    【解决方案3】:

    每次迭代你都在替换前一卷的数据。你可以将逻辑重写为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2012-09-28
      • 2011-07-12
      • 1970-01-01
      • 2011-01-19
      • 2019-05-09
      相关资源
      最近更新 更多