0

こんにちは、サイコロを振るゲームの頻度表を作成しようとしています。私が取り組んでいるプロジェクトの手順は次のとおりです。

標準の 6 面サイコロ (1 ~ 6 の番号) を振ることをシミュレートするアプリケーションを作成します。

  • さいころはちょうど 10,000 回振る必要があります。
  • 10,000 ロールはユーザーが入力する必要があります。サイコロをどのくらいの頻度で振りたいかを尋ねる
  • ロールされたサイコロの値は、Random クラス オブジェクトの出力に基づいてランダム値を使用して決定する必要があります (以下の注を参照)。
  • プログラムがユーザーが要求した回数 (10,000 回) の転がりを完了すると、アプリケーションは、各サイコロが転がった回数を表示するテーブルを表示する必要があります。
  • プログラムは、サイコロを転がす別のセッションをシミュレートするかどうかをユーザーに尋ねる必要があります。セッション数を追跡します。

これで、乱数クラスの使用方法はわかりましたが、プロジェクトの要約テーブルの部分で立ち往生しており、始めるのに役立つものが必要です

これが私がプロジェクトのこれまでのところどこにいるかです。ご覧のとおり、私の要約表は意味がありません。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Dice
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rndGen = new Random();

            Console.WriteLine("welcome to the ralph dice game");
            Console.Clear();

            Console.WriteLine("how many times do you want to roll");
            int rollDice = int.Parse(Console.ReadLine());

            for (int i = 0; i < rollDice; i++)
            {
                int diceRoll = 0;

                diceRoll = rndGen.Next(1,7);

                string table = " \tfrequency\tpercent";
                table +="\n"+ "\t" + i + "\t" + diceRoll;

                Console.WriteLine(table);

            }//end for

            Console.ReadKey();

        }

    }
}
4

1 に答える 1

0

各サイコロを振った回数を示す表を提示します。

私がこれを正しく理解していれば、サイコロが 1、2、3 などになる回数を意味します...すべての結果カウントを格納し、すべてのロールが完了したときに出力する配列が必要です。

注: テストされていないコード。

int[] outcomes = new int[6];

// init
for (int i = 0; i < outcomes.Length; ++i) {
    outcomes[i] = 0;
}

for (int i = 0; i < rollDice; i++)
{
    int diceRoll = 0;

    diceRoll = rndGen.Next(1,7);

    outcomes[diceRoll - 1]++; //increment frequency. 
    // Note that as arrays are zero-based, the " - 1" part turns the output range 
    // from 1-6 to 0-5, fitting into the array.

}//end for

// print the outcome values, as a table

セッション数を追跡します。

別の変数を使用するだけですが、コードにはこの部分が実装されていないようです。簡単な方法は、do-while ループを使用することです。

do {

    // your code

    // ask if user wish to continue

    bool answer = // if user want to continue

} while (!answer);
于 2013-03-09T05:41:39.320 に答える