C#Dice Times每个号码都被滚动

时间:2014-01-22 17:33:56

标签: c#

我创建了一个骰子程序,它根据用户希望滚动的次数来掷骰子。我遇到的问题是能够计算每个数字出现的次数。

实施例。滚两次 - 数字是5。                       数字是4。

我希望能够计算出4和5已经滚动并将其显示给用户。

我的代码到目前为止

using System;

namespace Dice
{
    class Dice
    {
        private Random randy;               
        int total;

        public static void Main()
        {
            Dice myDice = new Dice();       
            myDice.randy = new Random();    
            Console.Clear();                

            myDice.manyThrow();
            myDice.throwDice();
            Console.ReadKey();
        }

        //*******************************************************
        public void throwDice()
        {
            double count = 0;
            while(count != total)
            {
                count++;
                Console.WriteLine("Throw No " + count + " is " + oneThrow());    
            }
        }

        //*******************************************************
        public int oneThrow()
        {
            return randy.Next(6) + 1; // pick a number from 1 to 6 and return this
        }

        public int manyThrow()
        {
                Console.Write("How many times do you want to roll\t");
                total = Convert.ToInt32(Console.ReadLine());
                return total;
        }

        public void countEm()
        {
            // This is where I intend to place the code
        }    
    }
}

我正试图解决这个问题几个小时。

5 个答案:

答案 0 :(得分:2)

添加数组以计算滚动结果:

using System;

namespace Dice
{
    class Dice
    {
        private Random randy;
        int total;
        int [] rolls = new int[7];

        public static void Main()
        {
            Dice myDice = new Dice();
            myDice.randy = new Random();
            Console.Clear();

            myDice.manyThrow();
            myDice.throwDice();
            myDice.countEm();
            Console.ReadKey();

        }

        //*******************************************************
        public void throwDice()
        {
            double count = 0;
            while (count != total)
            {

                count++;
                Console.WriteLine("Throw No " + count + " is " + oneThrow());

            }
        }

        //*******************************************************
        public int oneThrow()
        {
            var result = randy.Next(6) + 1;        // pick a number from 1 to 6 and return this
            this.rolls[result]++;
            return result;

        }
        public int manyThrow()
        {

            Console.Write("How many times do you want to roll\t");
            total = Convert.ToInt32(Console.ReadLine());
            return total;

        }
        public void countEm()
        {
            for (int i = 1; i<7; i++)
            {
                Console.WriteLine("{0} rolled {1} times", i, rolls[i]);
            }
        }
    }
}

答案 1 :(得分:1)

像这样修改countEM:

public void countEm(int numberOfThrows)
{
      List<int> Throws = new List<int>(); //Create list, where value of throws will be stored.
      for(int i = 0; i < numberOfThrows) //Loop as many times as are given by parameter.
      {
          Throws.Add(OneThrow()); //Add return value of throw to the list.
      }
      //Show values in Throws list.
      foreach(var diceValue in Throws)
      {
           Console.WriteLine(string.Format("Value of throw: {0}", diceValue ));
      }
} 

coutEM应该用你想抛出的次数来调用:

countEM(5);

上面代表5次抛出。例如,结果可以填写:

  

1,4,6,3,4

答案 2 :(得分:1)

试试这个:

static int[] counts = new int[6];

oneThrow()

int result = randy.Next(6);
counts[result]++;
return result + 1;

然后在countEm()

for (int i = 0; i < counts.Length; i++)
{
    Console.WriteLine("{0}: {1}", i + 1, counts[i]);
}

答案 3 :(得分:1)

保持一系列可能的结果计数。

class Dice
{
    private Random randy;               
    private int total;
    private static const int NUM_SIDES = 6;
    private int[] counts;

    public Dice()
    {
        randy = new Random();
        counts = new int[ NUM_SIDES ];
    }

    public static void Main()
    {
        Dice myDice = new Dice();
        Console.Clear();                

        int throws = myDice.manyThrow(); // need to save the number of throws
        myDice.throwDice( throws );
        countEm();
        Console.ReadKey();
    }

    public void throwDice( int throws )
    {
        double count = 0;
        while(count != throws)
        {
            count++;
            int result = oneThrow();
            counts[ result - 1 ]++; // NOTE: result-1 because arrays are 0-based.
            Console.WriteLine("Throw No " + count + " is " + oneThrow());    
        }
    }

    // ...

    public void countEm()
    {
        for( int i = 0; i < counts.Count; ++i )
            Console.WriteLine( (i+1) + " thrown " + counts[i] + " times." );
    }

答案 4 :(得分:1)

如果您只想跟踪每个滚动数字的数量,那么您可以在类中添加一个整数数组,以存储每个滚动数字的计数。骰子卷的数量可以是数组的索引。

如果您需要跟踪滚动数字的顺序。您的班级可以使用链表或其他有序列表来跟踪数字。

这听起来像是家庭作业,所以我会将实施细节留给读者:)