【问题标题】:frequency table for dice c# console骰子 c# 控制台的频率表
【发布时间】:2013-03-09 05:31:31
【问题描述】:

您好,我正在尝试为掷骰子游戏创建频率表。以下是我正在从事的项目的说明:

创建一个模拟滚动标准 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();

        }

    }
}

【问题讨论】:

    标签: c# random


    【解决方案1】:

    展示一个表格,显示每个骰子的掷骰次数。

    如果我理解正确,这意味着骰子得到 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);
    

    【讨论】:

    • 这确实帮助了我,但我仍然不知道如何从数组中的骰子中调用这些数字并将它们放在桌子上。
    • 使用 for 循环读取数组。 for (int i = 0; i &lt; array.length; ++i) { Console.WriteLine(array[i]); }
    猜你喜欢
    • 2013-06-12
    • 2011-01-19
    • 2015-07-03
    • 2017-08-27
    • 2010-10-04
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多