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 回答