1

私の問題は、プログラムをコンパイルして実行しようとすると、コードの最後に近い算術問題の 1 つがゼロ除算であると表示されることです。今、別の問題があります。ユーザーがロールの数を入力するように求められた場合はいつでも、数値を入力して Enter キーを押すことができますが、次の行にスキップするだけで何も起こりません。コード内では他に何も起こりません。

* ノート *

次のセクションまでカバーされないため、この課題では配列を使用できません。

これが私の課題です。これが私がやるべきことです。ここで何が問題なのかわかりません。私の計算は正しいようですが、何かが間違っています。

要するに、私の課題では、2 つの 11 面ダイスがユーザーの入力回数だけ「転がされる」確率を求めています。例えば:

ユーザーがサイコロを 100 回振ると言うと、次のように出力されます。 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
4

2 に答える 2

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可能な合計があるので(2つのサイコロでは到達できません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 に答える
1

numberOfRolls開始を 0 ではなく 1 に変更します。

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

が 0 の場合numberOfRolls、すべての除算演算は 0 による除算になります。

probability2 = diceTotal2 / numberOfRolls;
于 2012-10-15T14:33:08.647 に答える