我是编程世界的新手,所以如果你们中的任何人能帮助我在这方面做得更好,我将不胜感激。我对这个程序的目标是模拟 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 + " timesnYou rolled double twos " + doubleTwos + " timesnYou"
+ " rolled double threes " + doubleThrees + " timesnYou rolled double fours " + doubleFours + " timesnYou"
+ " rolled double fives " + doubleFives + " timesnYou rolled double sixes " + doubleSixes + " times");
}
我在这里遇到的问题是,我从该程序中获得的结果似乎并不"合理"。例如,在 3000 个掷骰子中,我得到 1 对每个双打。我做错了什么?
您必须在代码中进行一些更改,
1. 通过 0
初始化所有变量。
2. 将while-loop
条件更改为
while (diceRolls <= finalDiceRoll) // hoping that finalDiceRoll = 3000
3 从while-loop
中删除int diceRolls = 1;
,并在while-loop
末尾添加diceRolls++;
。
4.把你的JOptionPane
放在while-loop
外面,如果没有,你必须关闭3000次你的JOptionPane
dailog。
将这些更改应用于代码后,您将了解自己做错了什么,并且代码将运行良好。
将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 + " timesnYou rolled double twos " + doubleTwos + " timesnYou"
+ " rolled double threes " + doubleThrees + " timesnYou rolled double fours " + doubleFours + " timesnYou"
+ " rolled double fives " + doubleFives + " timesnYou 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] + " timesnYou rolled double twos " + doubled[1] + " timesnYou"
+ " rolled double threes " + doubled[2] + " timesnYou rolled double fours " + doubled[3] + " timesnYou"
+ " rolled double fives " + doubled[4] + " timesnYou rolled double sixes " + doubled[5] + " times");
计数逻辑应如下所示:
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];
...
简化。消除重复。当可以使用数组时,不要使用多个变量。
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] + " timesn";
JOptionPane.showMessageDialog (null, output);
这就是您所需要的。