【问题标题】:dice rolling in C# console application giving incorrect totals在 C# 控制台应用程序中掷骰子给出不正确的总数
【发布时间】:2012-11-30 21:40:54
【问题描述】:

我正在 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();
    }
}

【问题讨论】:

  • 完成后请不要编辑您的问题。它应该留给其他人阅读,以便他们从中受益,并给出答案。

标签: c# dice


【解决方案1】:

在我看来你的数学倒退了……不应该是这样吗:

// 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
}

【讨论】:

  • 您好,我按照您的指示进行了更改,但仍然没有得到预期的输出。
  • “仅捕获计数”版本完美运行...是您正在尝试的版本吗?
【解决方案2】:

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

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

    // ... don't create a new random in `Roll` method. Use `rnd` directly.
}

【讨论】:

  • 您好,我按照您的指示进行了更改,但仍然没有得到预期的输出。
【解决方案3】:

首先,下面的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];

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

如果您以这种方式修改代码,您会提到您的分析系统不会以您实现它的方式工作。实际上,您展示的是每次投掷的结果,如果我没听错的话,这不是您想要的。

更新:

对不起,我误会你了。您确实希望显示每卷的结果。那么,为什么不直接将 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();
    } 
}

希望我能帮到你。

【讨论】:

    【解决方案4】:

    根据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();
        }
      }
    

    【讨论】:

    • 不鼓励将完整的代码发布到作业问题中。
    • 对不起。我没有注意到标签:-)
    • 虽然不是什么大问题;顺便说一句,编辑在修订历史记录中可见,因此实际上没有删除任何内容;-) 我正在回滚您之前的答案。
    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多