骰子在C#控制台应用程序中滚动,给出错误的总计

1 c# dice

我在C#console中编写骰子滚动程序.我给了两个输入

  1. 输入骰子的大小和
  2. 输入您想要播放的次数.

假设骰子大小是我玩过的6倍和10倍.

Output is coming:
1 was rolled  2 times
2 was rolled  7 times
3 was rolled  8 times
4 was rolled  7 times
5 was rolled  4 times
6 was rolled  5 times
Run Code Online (Sandbox Code Playgroud)

总计:33(每次执行都不固定,不会改变)

但是我的要求是这个总数应该是播放次数.在这里我正在玩10次,所以总数应该是10而不是33.这应该发生在每一个新的数字......如果我玩100次总和或总数应该是100而不是任何其他数字.休息一切都会保持不变,在我的计划中没有得到预期的金额.请有人修改它.这是我的代码:

Dice.cs:

public class Dice
{
    Int32 _DiceSize;
    public  Int32 _NoOfDice;     
    public Dice(Int32 dicesize)
    {
        this._DiceSize = dicesize;         
    }
    public string Roll()
    {
        if (_DiceSize<= 0)
        {
            throw new ApplicationException("Dice Size cant be less than 0 or 0");                 
        }
        if (_NoOfDice <= 0)
        {
            throw new ApplicationException("No of dice cant be less than 0 or 0");
        }
        Random rnd = new Random();
        Int32[] roll = new Int32[_DiceSize];
        for (Int32 i = 0; i < _DiceSize; i++)
        {
            roll[i] = rnd.Next(1,_NoOfDice);
        }
        StringBuilder result = new StringBuilder();
        Int32 Total = 0;
        Console.WriteLine("Rolling.......");
        for (Int32 i = 0; i < roll.Length; i++)
        {
            Total += roll[i];
            result.AppendFormat("{0}:\t was rolled\t{1}\t times\n", i + 1, roll[i]);
        }
        result.AppendFormat("\t\t\t......\n");
        result.AppendFormat("TOTAL: {0}", Total);
        return result.ToString();
    }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter no of dice size");
        int dicesize = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("How many times want to play");
        int noofplay=Convert.ToInt32(Console.ReadLine());
        Dice obj = new Dice(dicesize);
        obj._NoOfDice = noofplay;
        obj.Roll();
        Console.WriteLine(obj.Roll());           
        Console.WriteLine("Press enter to exit");
        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 5

它看起来像你正在向后推算数学...不应该是:

// to capture just the counts
int[] roll = new int[_DiceSize];
for (int i = 0; i < _NoOfDice; i++)
{
    roll[rnd.Next(roll.Length)]++;
}
Run Code Online (Sandbox Code Playgroud)

或者如果你想要实际的卷:

// to capture individual rolls
int[] roll = new int[_NoOfDice];
for (int i = 0; i < _NoOfDice; i++)
{
    roll[i] = rnd.Next(_DiceSize) + 1; // note upper bound is exclusive, so +1
}
Run Code Online (Sandbox Code Playgroud)