1

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

* 笔记 *

我不能在这个作业中使用数组,因为直到下一节才会介绍它。

这是我在这里的任务。这是我应该做的。我无法弄清楚这里出了什么问题。我的数学似乎是正确的,但出了点问题。

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

如果用户说骰子要滚动 100 次,它会输出如下内容 2s:(插入 100 次掷骰后 2 个骰子的总和为 2 的概率) 3s:(插入 2 个骰子的总和的概率100 次掷骰后骰子为 3)4 秒:(插入 100 次掷骰后 2 个骰子的总和为 4 的概率) 5 秒:(插入 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
4

2 回答 2

1

将启动更改numberOfRolls为 1 而不是 0。

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

如果numberOfRolls为 0,则所有除法运算都会导致除以零。

probability2 = diceTotal2 / numberOfRolls;
于 2012-10-15T14:33:08.647 回答
1

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

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]对应于 2hist[1]的总和,3 的总和等)。

最后,您可以计算百分比。(这不是概率,而是在我们的模拟中这个总和出现的百分比。如果你让掷骰子的数量更大,这个百分比将是概率的近似值。)

最后,您只需将其打印出来。这些String.format东西只是为了对齐值。如果您对此感到困惑,请使用

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

反而。

于 2012-10-15T15:24:12.230 回答