c#掷骰子模拟返回每侧降落了多少次

问题描述 投票:-1回答:2

我在这里的第一篇文章!几天前,我开始学习如何编程,并选择了滚动骰子项目。我想我已经完成了主要部分,但是我的问题来自:

打印每面多少次(也许将它们存储在阵列中?)?奖励:打印出每一面出现的百分比(四舍五入到小数点后两位)

请记住时间和方面是用户输入的内容,因此例如50次x 35面。

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;

namespace Dice_roll
{
    class Program
    {
        static void Main(string[] args)
        {
            static int getRandomNumber(int min, int max)
            {
                Random randomRoll = new Random();
                lock (randomRoll)
                {
                    return randomRoll.Next(min, max);
                }
            }

            int sides, rollTimes, i;
            i = 0;
            bool boolCheckSides = false;
            bool boolCheckRolls = false;
            Console.WriteLine("Welcome to a little dice rolling program.\n Select how many sides you want the dice to have: \n");
            do
            {
                boolCheckSides = Int32.TryParse(Console.ReadLine(), out sides);
                if (boolCheckSides == false)
                {
                    Console.WriteLine("Invalid number.. \n");
                }
            } while (boolCheckSides == false);

            Console.WriteLine("Now select how many times you want it to roll: \n");
            do
            {
                boolCheckRolls = Int32.TryParse(Console.ReadLine(), out rollTimes);
                if (boolCheckRolls == false)
                {
                    Console.WriteLine("Invalid number.. \n");
                }
            } while (boolCheckRolls == false);

            do
            {
                Console.WriteLine("Rolling... " + getRandomNumber(1, sides));
                i = i + 1;
                System.Threading.Thread.Sleep(500);
            } while (i < rollTimes);



            int[] arr = new int[rollTimes];
        }
    }
}

对不起,如果我的代码混乱或有些混乱,我仍然是个初学者。

c# arrays dice
2个回答
0
投票

嗯,我认为您的代码尚未完成,所以我只分享我的一些意见。

1-您正在主函数中添加方法getRandomNumber。该方法无法编译,应移至主要功能范围之外。主要功能的范围是以下代码中大括号“ {}”之间的任何代码

static void main(string[] args){}

只是一个小小的注释,请尝试使用大写字母作为C#中的命名方法,但主要功能除外,因为这是一个命名约定;-)

2-在开始分配变量值之前,您在声明所需变量(boolCheckSides,boolCheckRolls,i)方面做得很好。但是,您还需要先声明Array,然后再使用,因此您的代码应如下所示

//Declare the Array
int[] arr = new int[rollTimes];

do
{
    int temp = getRandomNumber(1, sides);
    Console.WriteLine("Rolling... " + temp);
    //Save the value acquired from this roll in the array
    arr[i] = temp;
    i = i + 1;
    System.Threading.Thread.Sleep(500);
} while (i < rollTimes);

然后,您可以轻松地在数组中四处查找并找到任意数字的频率。如果您想找到简短的答案,我建议您参考这篇文章Count the frequency of element of an array in C#,对于较长的答案,请参考这篇文章Frequency of Numbers in a 1D Array。请注意,第二篇文章使用的是C ++语言,但是仍然显示出相同的逻辑。

3-只是一个小建议,请尽量避免执行“ do ... while”循环。编写它们时,需要特别注意,因为它们很容易使您陷入无限循环。


0
投票

我建议您使用字典,将侧面(1到X)作为键,并在值中出现的次数。

class Program
{
    Dictionary<int, int> RollDice = new Dictionary<int, int>()
    static void Main(string[] args)
    {
        :
        Console.WriteLine("Welcome to a little dice rolling program.\n Select how many sides you want the dice to have: \n");
        do
        {
            boolCheckSides = Int32.TryParse(Console.ReadLine(), out sides);
            if (boolCheckSides == false)
            {
                Console.WriteLine("Invalid number.. \n");
            }
        } while (boolCheckSides == false);
        //Initialize the dictionary with key and value
        for(int i = 1; i<=sides;i++)
             RollDice.Add(i, 0);

       :

        do
        {
            var value = getRandomNumber(1, sides + 1)
            Console.WriteLine("Rolling... " + value);
            RollDice[i]++; 
            i = i + 1;
            System.Threading.Thread.Sleep(500);
        } while (i < rollTimes);
© www.soinside.com 2019 - 2024. All rights reserved.