我的分配指令是“滚动” 2 个骰子并得到总和,然后根据用户想要掷骰子的次数找到总和出现的概率。我必须使用嵌套循环,并且我不能为每个骰子组合使用单独的循环(我还没有这样做)。我不允许在此作业中使用数组。
编写一个程序来模拟掷一对 11 面骰子,并确定掷骰子的每种可能组合的次数百分比。
- 在 Mod05 Assignments 文件夹中创建一个名为 5.05 Random Dice 的新项目。
- 在新创建的项目文件夹中创建一个名为 DiceProbability 的类。
- 要求用户输入掷骰子的次数。
- 计算每个骰子组合的概率。(您可能想从更熟悉的六面骰子开始。)
- 将结果整齐地打印在两列中
我需要帮助找出我放入第二个“for”循环的内容。很抱歉整数列表和 if 语句的混乱。代码未完成。
import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
int count10 = 0;
int count11= 0;
int count12= 0;
int count13 = 0;
int count14 = 0;
int count15 = 0;
int count16 = 0;
int count17 = 0;
int count18 = 0;
int count19 = 0;
int count20 = 0;
int count21 = 0;
int count22 = 0;
int die1 = 0, die2 = 0;
int rolls = 0;
int actualDiceSum;
double probabilityOfDice = 0.0;
System.out.print("Number of Rolls: ");
rolls = in.nextInt();
for(int timesRolled = 0; timesRolled < rolls; timesRolled++)
{
die1 = randNum.nextInt(12);
die2 = randNum.nextInt(12);
actualDiceSum = die1 + die2;
for()
{
if(actualDiceSum == 2){
count2++;
probabilityOfDice = count2 / rolls;
}
else if(actualDiceSum == 3){
count3++;
probabilityOfDice = count3 / rolls;
}
else if(actualDiceSum == 4){
count4++;
probabilityOfDice = count4 / rolls;
}
else if(actualDiceSum == 5){
count5++;
probabilityOfDice = count5 / rolls;
}
else if(actualDiceSum == 6){
count6++;
probabilityOfDice = count6 / rolls;
}
else if(actualDiceSum == 7){
count7++;
probabilityOfDice = count7 / rolls;
}
else if(actualDiceSum == 8){
count8++;
probabilityOfDice = count8 / rolls;
}
else if(actualDiceSum == 9){
count9++;
probabilityOfDice = count9 / rolls;
}
else if(actualDiceSum == 10){
count10++;
probabilityOfDice = count10 / rolls;
}
else if(actualDiceSum == 11){
count11++;
probabilityOfDice = count11 / rolls;
}
else if(actualDiceSum == 12){
count12++;
probabilityOfDice = count12 / rolls;
}
else if(actualDiceSum == 13){
count13++;
probabilityOfDice = count13 / rolls;
}
else if(actualDiceSum == 14){
count14++;
probabilityOfDice = count14 / rolls;
}
else if(actualDiceSum == 15){
count15++;
probabilityOfDice = count15 / rolls;
}
else if(actualDiceSum == 16){
count16++;
probabilityOfDice = count16 / rolls;
}
else if(actualDiceSum == 17){
count17++;
probabilityOfDice = count17 / rolls;
}
else if(actualDiceSum == 18){
count18++;
probabilityOfDice = count18 / rolls;
}
else if(actualDiceSum == 19){
count19++;
probabilityOfDice = count19 / rolls;
}
else if(actualDiceSum == 20){
count20++;
probabilityOfDice = count20 / rolls;
}
else if(actualDiceSum == 21){
count21++;
probabilityOfDice = count21 / rolls;
}
else if(actualDiceSum == 22){
count22++;
probabilityOfDice = count22 / rolls;
}
}
}
System.out.println("Sum of Dice \t\t Probability");
System.out.println("2's\t\t" + probabilityOfDice + "%");
System.out.println("3's\t\t" + probabilityOfDice + "%");
System.out.println("4's\t\t" + probabilityOfDice + "%");
System.out.println("5's\t\t" + probabilityOfDice + "%");
System.out.println("6's\t\t" + probabilityOfDice + "%");
System.out.println("7's\t\t" + probabilityOfDice + "%");
System.out.println("8's\t\t" + probabilityOfDice + "%");
System.out.println("9's\t\t" + probabilityOfDice + "%");
System.out.println("10's\t\t" + probabilityOfDice + "%");
System.out.println("11's\t\t" + probabilityOfDice + "%");
System.out.println("12's\t\t" + probabilityOfDice + "%");
System.out.println("13's\t\t" + probabilityOfDice + "%");
System.out.println("14's\t\t" + probabilityOfDice + "%");
System.out.println("15's\t\t" + probabilityOfDice + "%");
System.out.println("16's\t\t" + probabilityOfDice + "%");
System.out.println("17's\t\t" + probabilityOfDice + "%");
System.out.println("18's\t\t" + probabilityOfDice + "%");
System.out.println("19's\t\t" + probabilityOfDice + "%");
System.out.println("20's\t\t" + probabilityOfDice + "%");
System.out.println("21's\t\t" + probabilityOfDice + "%");
System.out.println("22's\t\t" + probabilityOfDice + "%");
}
}