java - 在 Java 中使用嵌套 for 循环查找概率

标签 java if-statement for-loop probability nested-loops

我的问题是,每当我尝试编译和运行我的程序时,它都会说我接近代码末尾的算术问题之一被零除。现在还有另一个问题。每当提示用户输入掷骰数时,您可以输入一个数字并按 Enter 键,但它只是跳到下一行并且没有任何反应。代码中没有发生任何其他事情。

* 注意*

我无法在此作业中使用数组,因为直到下一部分才会介绍它。

这是我的作业 here 。这就是我应该做的。我不明白这里出了什么问题。我的数学似乎是正确的,但有些地方出了问题。

简而言之,我的作业希望我找到两个 11 面骰子被“滚动”用户输入次数的概率。例如:

如果用户说骰子要掷 100 次,它会输出如下内容 2s:(插入 100 次掷骰后 2 个骰子的总和为 2 的概率) 3s:(插入 100 次掷骰后 2 个骰子的总和为 3 的概率) 4s:(插入 100 次掷骰后 2 个骰子的总和为 4 的概率) 5s:(插入100次掷骰后2个骰子的总和为5的概率)

等等。

这是迄今为止我的代码:

public static void main(String[] args)

{

    //Declare and initialize variables and objects

    Scanner in = new Scanner(System.in);

    Random randNum = new Random();




    int match = 0; //Number of times sum of dice matches the current sum

    int die1 = 0; //Random generated numbers
    int die2 = 0;
    int diceTotal2 = 0;
    int diceTotal3 = 0;
    int diceTotal4 = 0;
    int diceTotal5 = 0;
    int diceTotal6 = 0;
    int diceTotal7 = 0;
    int diceTotal8 = 0;
    int diceTotal9 = 0;
    int diceTotal10 = 0;
    int diceTotal11 = 0;
    int diceTotal12 = 0;

    int sumOfDice = 0;
    double probability2 = 0.0;
    double probability3 = 0.0;
    double probability4 = 0.0;
    double probability5 = 0.0;
    double probability6 = 0.0;
    double probability7 = 0.0;
    double probability8 = 0.0;
    double probability9 = 0.0;
    double probability10 = 0.0;
    double probability11 = 0.0;
    double probability12 = 0.0;


    //Input: ask user for number of rolls and number of sides on a die

    System.out.println("Number of Rolls: ");

    int rolls = in.nextInt();

    //***************************************************************************************

    //Using nested loops, cycle through the possible sums of the dice.

    //Roll the dice the given number of times for each sum.

    //Count how many times the sum of the dice match the current sum being looked for.

    //***************************************************************************************


    //Loop to increment through the possible sums of the dice

        //Loop to throw dice given number of times

        for( int numberOfRolls = 1; numberOfRolls < rolls; numberOfRolls++)

        {
              die1 = randNum.nextInt(6);
              die2 = randNum.nextInt(6);
              sumOfDice = die1 + die2;
              for( ; ; )
              {

            //Check if the sum of dice is equal to the given sum
                if(sumOfDice == 2)

                {
                    diceTotal2++;
                    probability2 = diceTotal2 / numberOfRolls;
                }

                else if(sumOfDice ==3)
                {
                    diceTotal3++;
                    probability3 = diceTotal3 / numberOfRolls;
                }

                else if(sumOfDice ==4)
                {
                    diceTotal4++;
                    probability4 = diceTotal4 / numberOfRolls;
                }

                else if(sumOfDice ==5)
                {
                    diceTotal5++;
                    probability5 = diceTotal5 / numberOfRolls;
                }

                else if(sumOfDice ==6)
                {
                    diceTotal6++;
                    probability6 = diceTotal6 / numberOfRolls;
                }

                else if(sumOfDice ==7)
                {
                    diceTotal7++;
                    probability7 = diceTotal7 / numberOfRolls;
                }

                else if(sumOfDice ==8)
                {
                    diceTotal8++;
                    probability8 = diceTotal8 / numberOfRolls;
                }

                else if(sumOfDice ==9)
                {
                    diceTotal9++;
                    probability9 = diceTotal9 / numberOfRolls;
                }

                else if(sumOfDice ==10)
                {
                    diceTotal10++;
                    probability10 = diceTotal10 / numberOfRolls;
                }

                else if(sumOfDice ==11)
                {
                    diceTotal11++;
                    probability11 = diceTotal11 / numberOfRolls;
                }

                else if(sumOfDice ==12)
                {
                    diceTotal12++;
                    probability12 = diceTotal12 / numberOfRolls;
                }

            }                

        }



       System.out.println("Sum of Dice" + "         " + "Probability");
       System.out.println("2s: \t\t" + probability2 + "%");
       System.out.println("3s: \t\t" + probability3 + "%");
       System.out.println("4s: \t\t" + probability4 + "%");
       System.out.println("5s: \t\t" + probability5 + "%");
       System.out.println("6s: \t\t" + probability6 + "%");
       System.out.println("7s: \t\t" + probability7 + "%");
       System.out.println("8s: \t\t" + probability8 + "%");
       System.out.println("9s: \t\t" + probability9 + "%");
       System.out.println("10s: \t\t" + probability10 + "%");
       System.out.println("11s: \t\t" + probability11 + "%");
       System.out.println("12s: \t\t" + probability12 + "%");


        //After all throws, calculate percentage of throws that resulted in the given sum



} //end main

最佳答案

由于您已经有了一个解决方案,我将向您提供另一个解决方案:

import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int nRolls = 100, nDice = 6; // default values

        Scanner in = new Scanner(System.in);
        System.out.print("Number of throws: ");
        nRolls = in.nextInt();
        System.out.print("Number of sides on the dices: ");
        nDice = in.nextInt();

        int minSum = 2, maxSum = 2 * nDice;
        int[] hist = new int[maxSum - minSum + 1];

        Random rand = new Random();
        for (int iter = 1; iter <= nRolls; iter++) {
            int throw1 = 1 + rand.nextInt(nDice), throw2 = 1 + rand.nextInt(nDice);
            int sum = throw1 + throw2;
            hist[sum - minSum]++;
        }

        System.out.println("Number of rolls: " + nRolls);
        System.out.println("Number of sides of the dice: " + nDice);
        System.out.println("Sum of Dice         Percentage");
        for (int i = 0; i < hist.length; i++) {
            System.out.println(String.format("   %2d                 %5.2f%%", i + minSum, hist[i] * 100.0 / nRolls));
            // System.out.println("   " + (i+minSum) + "             " + (hist[i]*100.0/nRolls);
        }

        in.close();
    }
}

它向您展示了如何使用数组来解决此任务。数组中的每个条目都保存与相应总和相关的抛出次数。您始终有 2*nDice - 1 可能的总和(您无法用两个骰子达到 1),因此数组的大小取决于骰子上的边数骰子。

然后你只需迭代所有抛出,并将 1 添加到相应的直方图条目(请注意,我偏移了直方图,因此 hist[0] 对应于 2 的总和,hist[ 1] 总和为 3,等等)。

最后,您可以计算百分比。 (这不是概率,而是在我们的模拟中该总和发生的百分比。如果您将掷骰数设置得更大,则该百分比将是概率的近似值。)

最后,您只需将其打印出来即可。 String.format 内容仅用于值的对齐。如果你对此感到困惑,只需使用

System.out.println("   " + (i+minSum) + "             " + (hist[i]*100.0/nRolls);

相反。

关于java - 在 Java 中使用嵌套 for 循环查找概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897848/

相关文章:

Java检查文件大小是否大于0

javascript - 具有嵌套 forEach 和 for 循环的函数不会返回 false

java - 以中文显示整个 JAVA Web 应用程序

java - Java 中 BigDecimal 的 BigInteger 幂

c# - "if"不能完美工作

c++ - C++中的流程控制构造,如何修改?

c 编程 for 循环输入字符

java - 参数化对象无法解析为变量

java - 不关闭 URLConnection

c - 具有相同条件的 while 循环内的 if 语句