如何模拟3000个骰子卷并计算双打的次数? JAVA

时间:2017-02-05 00:14:15

标签: java dice

我是编程世界的新手,所以如果你们中的任何人能够帮助我在这方面变得更好,我将深表感谢。我的这个程序的目标是模拟3000个骰子卷并使用while循环计算每个不同可能的双精度对的双倍滚动次数。结果应打印在对话框中。

   Random random;
   random = new Random();
   diceRolls = 1;
   snakeEyes = 1;
   doubleTwos = 1;
   doubleThrees = 1;
   doubleFours = 1;
   doubleFives = 1;
   doubleSixes = 1;


   while (diceRolls <= finalDiceRoll) {
       int diceRolls = 1;
       int die1 = random.nextInt(6) + 1;
       int die2 = random.nextInt(6) + 1;


       if (die1 == 1 && die2 == 1){
           //snakeEyes = snakeEyes + 1;    
           snakeEyes++;

       }
       else if (die1 == 2 && die2 == 2 ) {
           doubleTwos++;
       }
       else if (die1 == 3 && die2 == 3) {
           doubleThrees++;
       }
       else if (die1 == 4 && die2 == 4) {
           doubleFours++;
       }
       else if (die1 == 5 && die2 == 5) {
           doubleFives++;
       }
       else if (die1 == 6 && die2 == 6) {
           doubleSixes++;
       }
       JOptionPane.showMessageDialog (null, "You rolled snake eyes " + snakeEyes + " times\nYou rolled double twos " + doubleTwos + " times\nYou"
                                     + " rolled double threes " + doubleThrees + " times\nYou rolled double fours " + doubleFours + " times\nYou"
                                     + " rolled double fives " + doubleFives + " times\nYou rolled double sixes " + doubleSixes + " times");

}

我在这里遇到的问题是,我从该计划中获得的结果似乎并不合理[&34]。例如,在3000个骰子卷中,我得到1对每双。我做错了什么?

4 个答案:

答案 0 :(得分:1)

showMessageDialog移到while循环之外并递增diceRolls变量。用0初始化所有其他整数变量 更好(更清洁和更短)的方法是使用二维数组或地图。

Random random = new Random();
int snakeEyes = 0;
int doubleTwos = 0;
int doubleThrees = 0;
int doubleFours = 0;
int doubleFives = 0;
int doubleSixes = 0;
int diceRolls = 1;
while (diceRolls <= 3000) {
    int die1 = random.nextInt(6) + 1;
    int die2 = random.nextInt(6) + 1;

    if (die1 == 1 && die2 == 1) {
        snakeEyes++;
    } else if (die1 == 2 && die2 == 2) {
        doubleTwos++;
    } else if (die1 == 3 && die2 == 3) {
        doubleThrees++;
    } else if (die1 == 4 && die2 == 4) {
        doubleFours++;
    } else if (die1 == 5 && die2 == 5) {
        doubleFives++;
    } else if (die1 == 6 && die2 == 6) {
        doubleSixes++;
    }
    diceRolls++;
}
JOptionPane.showMessageDialog(null, "You rolled snake eyes " + snakeEyes + " times\nYou rolled double twos " + doubleTwos + " times\nYou"
        + " rolled double threes " + doubleThrees + " times\nYou rolled double fours " + doubleFours + " times\nYou"
        + " rolled double fives " + doubleFives + " times\nYou rolled double sixes " + doubleSixes + " times");

上述较短版本:

Random random = new Random();
int[] doubled = new int[6];

for (int diceRolls = 0; diceRolls < 3000; diceRolls++) {
    int die1 = random.nextInt(6);
    int die2 = random.nextInt(6);

    if (die1 == die2)
        doubled[die1]++;
}
JOptionPane.showMessageDialog(null, "You rolled snake eyes " + doubled[0] + " times\nYou rolled double twos " + doubled[1] + " times\nYou"
        + " rolled double threes " + doubled[2] + " times\nYou rolled double fours " + doubled[3] + " times\nYou"
        + " rolled double fives " + doubled[4] + " times\nYou rolled double sixes " + doubled[5] + " times");

答案 1 :(得分:1)

您必须在代码中进行少量更改

1。0初始化所有变量。

2。while-loop条件更改为

 while (diceRolls <= finalDiceRoll)  // hoping that finalDiceRoll = 3000

3 int diceRolls = 1;移除while-loop并在diceRolls++;末尾添加while-loop

4。JOptionPane放在while-loop之外。如果不是,你必须关闭3000 JOptionPane dailog。

将这些更改应用到您的代码后,您将了解您做错了什么,并且您的代码运行正常。

答案 2 :(得分:0)

计数逻辑应如下:

doubleTwos = 0;
doubleThrees = 0;
doubleFours = 0;
doubleFives = 0;
doubleSixes = 0;

finalDiceRoll = 3000;
int diceRolls = 0;
while (diceRolls < finalDiceRoll) {
    ++diceRolls;

在循环之后发送消息。

使用数组,您可以节省输入:

int[] doubleCounts = new int[6]; // By 0 based dice values
for (int i = 0; i < finalDiceRoll; ++i) {
    int die1 = random.nextInt(6); // With dice values 0-5
    int die2 = random.nextInt(6);

    if (die1 == die2) {
       ++doubleCounts[die1];
    }
}
int snakeEyes = doubleCounts[0];
int doubleTwos = doubleCounts[1];
...

答案 3 :(得分:0)

简化。消除重复。当你可以使用数组时,不要使用多个变量。

int[] doubles = new int[7]; // make the index the number rolled (ignore index 0)
int diceRolls = 0;
while (diceRolls++ < 3000) {
  int die1 = random.nextInt(6) + 1;
  int die2 = random.nextInt(6) + 1;
  if (die1 == die2)
       doubles[die1]++;
}
String output = "";
for (int i = 1; i <= 6; i++)
    output += "You rolled double " + i + "'s " + doubles[i] + " times\n";

JOptionPane.showMessageDialog (null, output);

这就是你所需要的一切。