1

我正在 C# 控制台中编写掷骰子程序。我给了两个输入

  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

总计:33(它不是每次执行都固定的,这个不会改变)

但我的要求是这个总数应该是播放次数。在这里我玩了 10 次,所以总数应该是 10 而不是 33。它应该发生在每个新数字上……如果我玩 100 次,总和或总数应该是 100,而不是任何其他数字。其余的一切都将保持不变,在我的编程中没有得到预期的总和。请有人修改它。这是我的代码:

骰子.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();
    }
}
4

4 回答 4

5

在我看来,您的数学倒退了……不应该是:

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

或者,如果您想要实际的卷:

// 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
}
于 2009-07-27T08:13:39.690 回答
2

您正在Random每次迭代中创建一个新实例。这不是一件好事,因为它会影响结果的均匀分布。将Random实例保存在字段中,而不是每次都创建一个新实例。

public class Dice {
    private Random rnd = new Random();

    // ... don't create a new random in `Roll` method. Use `rnd` directly.
}
于 2009-07-27T08:11:59.133 回答
1

首先,下面的for循环是错误的:

for (Int32 i = 0; i < _DiceSize; i++)
{
   roll[i] = rnd.Next(1,_NoOfDice);
}

显然你切换了 _DiceSize 和 _NoOfDice。正确的循环看起来像

for (Int32 i = 0; i < _NoOfDice; i++)
{
   roll[i] = rnd.Next(1,_DiceSize);
}

正因为如此,这条线

Int32[] roll = new Int32[_DiceSize];

必须改为

Int32[] roll = new Int32[_NoOfDice];

也许你应该考虑重命名这些变量,这样更清楚,这意味着什么。

如果您以这种方式修改您的代码,您会提到您的 analisys 不会以您实现它的方式工作。实际上,如果我理解正确的话,您所展示的是每次投掷的结果,这不是您想要的。

更新:

对不起,我误会你了。您确实希望显示每卷的结果。那么,为什么不直接将 StringBuilder.AppendFormat 移动到“滚动换循环”中呢?

更新#2:

对我来说,以下 Die-class 完全按照您想要的方式工作:

public class Die
{
    private int maxValue;
    private int numberOfRolls;
    private Random random;

    public Die(int maxValue, int numberOfRolls)
    {
        this.maxValue = maxValue;
        this.numberOfRolls = numberOfRolls;
        this.random = new Random();
    }

    public string Roll()
    {
        StringBuilder resultString = new StringBuilder();

        for (int i = 0; i < this.numberOfRolls; i++)
        {
            resultString.AppendFormat("Roll #{0} - Result: {1}" + Environment.NewLine, i + 1, this.random.Next(1, maxValue + 1));
        }

        return resultString.ToString();
    } 
}

希望我能帮助你。

于 2009-07-27T08:37:21.623 回答
0

根据MehrdadMarc Gravell的说法,这是您必须使用的完整代码。玩得开心。

  public class Dice
  {
    private Random rnd = new Random();

    Int32 _DiceSize;
    public Int32 _NoOfDice;
    public Dice(Int32 dicesize)
    {
      if (dicesize <= 0)
      {
        throw new ApplicationException("Dice Size cant be less than 0 or 0");
      }

      this._DiceSize = dicesize;
    }
    public string Roll()
    {

      if (_NoOfDice <= 0)
      {
        throw new ApplicationException("No of dice cant be less than 0 or 0");
      }
      // to capture just the counts
      int[] roll = new int[_DiceSize];
      for (int i = 0; i < _NoOfDice; i++)
      {
        roll[rnd.Next(roll.Length)]++;
      }
      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;
      Console.WriteLine(obj.Roll());
      Console.WriteLine("Press enter to exit");
      Console.ReadKey();
    }
  }
于 2009-07-27T08:31:18.360 回答