1

我的分配指令是“滚动” 2 个骰子并得到总和,然后根据用户想要掷骰子的次数找到总和出现的概率。我必须使用嵌套循环,并且我不能为每个骰子组合使用单独的循环(我还没有这样做)。我不允许在此作业中使用数组。

编写一个程序来模拟掷一对 11 面骰子,并确定掷骰子的每种可能组合的次数百分比。

  1. 在 Mod05 Assignments 文件夹中创建一个名为 5.05 Random Dice 的新项目。
  2. 在新创建的项目文件夹中创建一个名为 DiceProbability 的类。
  3. 要求用户输入掷骰子的次数。
  4. 计算每个骰子组合的概率。(您可能想从更熟悉的六面骰子开始。)
  5. 将结果整齐地打印在两列中

我需要帮助找出我放入第二个“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 + "%");
}
}
4

4 回答 4

0

我认为您需要使用二维数组,如下所示:

int rolls = 0;
System.out.print("Number of Rolls: ");
rolls = in.nextInt();

int[][] numAndOccuranceCount= new int[11][2]; //sum will be between 2 -12

//initialize your array
for(int indx=0; indx<11;indx++){
   numAndOccuranceCount[indx] = new int[]{indx+2,0);
} 

for(int timesRolled = 0; timesRolled < rolls; timesRolled++){
    die1 = randNum.nextInt(12);
    die2 = randNum.nextInt(12);
    actualDiceSum = die1 + die2;

    for(int indx=0; indx<11;indx++){
       if(actualDiceSum == numAndOccuranceCount[i][0]){
          numAndOccuranceCount[indx][1] = numAndOccuranceCount[indx][1]+1;
          break;
       }
    }
 }

 double proabability = 0.0;
 //compute and print the probablity as below:
  for(int indx=0; indx<11;indx++){
     proabability = numAndOccuranceCount[indx][1]/rolls;
     System.out.println("Probability of "+ numAndOccuranceCount[indx][0] +" = "+proabability);
  }
于 2012-10-12T03:50:59.530 回答
0

我对你的代码有一些评论。

根据说明,您总结 dice1 和 dice2 不是正确的方法,因为 1 + 5 = 2 + 4 = 3 + 3 但它们是不同的。所以我们必须根据组合而不是总和来计算可能性。

这是我的代码:

DTO类:

public class DiceCombination {
    private int dice1;
    private int dice2;

    public DiceCombination(int dice1, int dice2) {
        this.dice1 = dice1;
        this.dice2 = dice2;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Combination of [dice " + dice1 + " and dice " + dice2 + "]";
    }
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
    final int prime = 31;
    if(dice1 < dice2) {
        return prime*dice1 + dice2;
    }
    else {
        return prime*dice2 + dice1;
    }
}
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DiceCombination other = (DiceCombination) obj;
        if ((dice1 == other.dice1 && dice2 == other.dice2) || (dice1 == other.dice2 && dice2 == other.dice1))
            return true;
        return false;
    }

}

主类:

public class Posibility {

 public static void main(String[] args) {
    Map<DiceCombination,Integer> possibility = new HashMap<DiceCombination,Integer>();
    int dice1;
    int dice2;
    int roll = 400; // value here should be inputted from user, you may change any value you want
    Random randNum = new Random();

    for(int i = 0; i < roll; i ++) {
        dice1 = randNum.nextInt(12);
        dice2 = randNum.nextInt(12);
        DiceCombination dc = new DiceCombination(dice1, dice2);
        if(possibility.containsKey(dc)) {
            possibility.put(dc , possibility.get(dc) + 1); 
        }
        else {
            possibility.put(dc, 1);
        }
    }
    for(DiceCombination key : possibility.keySet()) {
        System.out.println("Result: " + key.toString() + " is " + ((double)possibility.get(key)/roll));
    }
 }
}
于 2012-10-12T04:31:48.870 回答
0

我同意@Thinhbk 的观点,即这个问题在技术上是用组合而不是总和来表达的,但链接中的图片显示总和,所以我相信总和是意图。您编写的组合方法令人印象深刻,但对于似乎是 Java 入门课程的课程来说太高级了。我喜欢图片显示 IDE 是 BlueJ 的方式。带我回去。无论如何,赋值描述并没有指定内部 for 循环的需要。我看到@user1713336 的编辑在您的(user1713336's)第一个循环之前不允许使用数组,您可以添加:

int[] count = new int[23];
//0 is the default value for each
//int[22] is the 23rd value in the array

您的整个内部 for 循环可以替换为:

count[actualDiceSum]++;

然后在 for 循环中打印时,打印count[i]/rolls*100并从int i可能的最低值 2 开始。我怀疑你接下来会学习数组,所以我想展示它是如何完成的。

由于您不能使用数组,因此您的countx = 0;行就可以了。不要实时计算概率。这是不必要的,并且添加了许多代码行,只需跟踪每个值的滚动次数,因此:

if(actualDiceSum == 2)
    count2++;
else if(actualDiceSum == 3)
    count3++;
//etc 

{}请注意,如果只有一行代码与 if 或 while 语句相关联,如图所示,则不需要使用。然后以与以前相同的方式打印count2/rolls*100(乘以 100,因为图片显示的是百分比,而不是比率)。

于 2012-10-12T04:50:42.397 回答
0

好吧,根据我的说法,您不需要第二个 for 循环。每次掷骰子时,您都会计算总和并根据总和增加计数。

您需要使用单独的变量来计算每个总和的概率。

例如

Probability of count2 = (count2/number of rolls);
Probability of count3 = (count2/number of rolls);

对计数概率使用单独的变量

试试这个代码

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;

        if(actualDiceSum == 2){
        count2++;

        }
        else if(actualDiceSum == 3){
        count3++;

        }
        else if(actualDiceSum == 4){
        count4++;

        }
        else if(actualDiceSum == 5){
        count5++;

        }
        else if(actualDiceSum == 6){
        count6++;

        }
        else if(actualDiceSum == 7){
        count7++;

        }
        else if(actualDiceSum == 8){
        count8++;

        }
        else if(actualDiceSum == 9){
        count9++;

        }
        else if(actualDiceSum == 10){
        count10++;

        }
        else if(actualDiceSum == 11){
        count11++;

        }
        else if(actualDiceSum == 12){
        count12++;

        }
        else if(actualDiceSum == 13){
        count13++;

        }
        else if(actualDiceSum == 14){
        count14++;

        }
        else if(actualDiceSum == 15){
        count15++;

        }
        else if(actualDiceSum == 16){
        count16++;

        }
        else if(actualDiceSum == 17){
        count17++;

        }
        else if(actualDiceSum == 18){
        count18++;

        }
        else if(actualDiceSum == 19){
        count19++;

        }
        else if(actualDiceSum == 20){
        count20++;

        }
        else if(actualDiceSum == 21){
        count21++;

        }
        else if(actualDiceSum == 22){
        count22++;

       }
    }
}


System.out.println("Sum of Dice \t\t Probability");
System.out.println("2's\t\t" + count2/rolls + "%");
System.out.println("3's\t\t" + count3/rolls + "%");
System.out.println("4's\t\t" + count4/rolls + "%");
System.out.println("5's\t\t" + count5/rolls + "%");
//and so on...
}
}
于 2012-10-12T04:52:22.800 回答