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

C#控制台应用程序中的骰子滚动给出错误的总数,c#,dice,C#,Dice,我正在C#控制台中编写掷骰子程序。我提供两个输入 输入骰子的大小,然后 输入您要播放的时间 假设骰子大小是我玩过的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(并非每次执行都固定此编号将被更改) 但我的要求是,这个总数

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

  • 输入骰子的大小,然后
  • 输入您要播放的时间 假设骰子大小是我玩过的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,而不是其他任何数字。其余的都将保持不变,在我的编程中没有得到预期的总和。请有人修改一下。这是我的密码:

    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();
        }
    }
    
    公共类骰子
    {
    Int32(大小);;
    公共国际会议;
    公共骰子(Int32骰子大小)
    {
    这个。_DiceSize=DiceSize;
    }
    公共字符串滚动()
    {
    
    如果每次迭代都要创建一个新的
    Random
    实例。这不是一件好事,因为它会影响结果的均匀分布。请将
    Random
    实例保留在字段中,而不是每次都创建一个新实例

    public class Dice {
        private Random rnd = new Random();
    
        // ... don't create a new random in `Roll` method. Use `rnd` directly.
    }
    

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

    public class Dice {
        private Random rnd = new Random();
    
        // ... don't create a new random in `Roll` method. Use `rnd` directly.
    }
    

    在我看来,你的数学似乎在倒退……是不是:

    // to capture just the counts
    int[] roll = new int[_DiceSize];
    for (int i = 0; i < _NoOfDice; i++)
    {
        roll[rnd.Next(roll.Length)]++;
    }
    
    //仅捕获计数
    int[]滚动=新int[_DiceSize];
    对于(int i=0;i<\u NoOfDice;i++)
    {
    滚动[rnd.Next(滚动长度)]+;
    }
    
    或者,如果您想要实际卷:

    // 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
    }
    
    //捕获单个卷
    int[]滚动=新int[_NoOfDice];
    对于(int i=0;i<\u NoOfDice;i++)
    {
    roll[i]=rnd.Next(_DiceSize)+1;//注意上界是独占的,所以+1
    }
    
    在我看来,你的数学似乎在倒退……难道不是:

    // to capture just the counts
    int[] roll = new int[_DiceSize];
    for (int i = 0; i < _NoOfDice; i++)
    {
        roll[rnd.Next(roll.Length)]++;
    }
    
    //仅捕获计数
    int[]滚动=新int[_DiceSize];
    对于(int i=0;i<\u NoOfDice;i++)
    {
    滚动[rnd.Next(滚动长度)]+;
    }
    
    或者,如果您想要实际卷:

    // 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
    }
    
    //捕获单个卷
    int[]滚动=新int[_NoOfDice];
    对于(int i=0;i<\u NoOfDice;i++)
    {
    roll[i]=rnd.Next(_DiceSize)+1;//注意上界是独占的,所以+1
    }
    
    根据和,这是您必须使用的完整代码。 玩得开心

      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();
        }
      }
    
    公共类骰子
    {
    私有随机rnd=新随机();
    Int32(大小);;
    公共国际会议;
    公共骰子(Int32骰子大小)
    {
    
    根据和,如果(dicesize这是您必须使用的完整代码。 玩得开心

      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();
        }
      }
    
    公共类骰子
    {
    私有随机rnd=新随机();
    Int32(大小);;
    公共国际会议;
    公共骰子(Int32骰子大小)
    {
    
    如果(dicesize首先,以下for循环错误:

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

    Int32[] roll = new Int32[_NoOfDice];
    
    也许你应该考虑重命名这些变量,这样更清楚,这意味着什么

    如果你以这种方式修改代码,你会提到你的分析不会以你实现它的方式工作。实际上,你展示的是每次抛出的结果,如果我理解正确的话,这不是你想要的

    更新:

    抱歉,我误解了您的意思。您确实希望显示每次滚动的结果。那么,为什么不将StringBuilder.AppendFormat移到“滚动循环”中呢

    更新#2:

    对我来说,以下模具类的工作方式正是您想要的:

    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();
        } 
    }
    
    公共类死亡
    {
    私有整数最大值;
    私人整数卷;
    私有随机;
    公共骰子(int maxValue,int numberOfRolls)
    {
    this.maxValue=maxValue;
    this.numberOfRolls=numberOfRolls;
    this.random=新的random();
    }
    公共字符串滚动()
    {
    StringBuilder resultString=新建StringBuilder();
    for(int i=0;i

    希望我能帮助您。

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

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

    Int32[] roll = new Int32[_NoOfDice];
    
    也许你应该考虑重命名这些变量,这样更清楚,这意味着什么

    如果你以这种方式修改代码,你会提到你的分析不会以你实现它的方式工作。实际上,你展示的是每次抛出的结果,如果我理解正确的话,这不是你想要的

    更新:

    抱歉,我误解了您的意思。您确实希望显示每次滚动的结果。那么,为什么不将StringBuilder.AppendFormat移到“滚动循环”中呢

    更新#2:

    对我来说,以下模具类的工作方式正是您想要的:

    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();
        } 
    }
    
    公共类死亡
    {
    私有整数最大值;
    私人整数卷;
    私有随机;
    公共骰子(int maxValue,int numberOfRolls)
    {
    this.maxValue=maxValue;
    this.numberOfRolls=numberOfRolls;
    this.random=新的random();
    }
    公共字符串滚动()
    {
    StringBuilder resultString=新建StringBuilder();
    for(int i=0;i

    希望我能帮助您。

    您好,我按照您的指示进行了更改,但仍然没有获得预期的输出。您好,我按照您的指示进行了更改,但仍然没有获得预期的输出。您好,我按照您的指示进行了更改,但仍然没有获得预期的输出。“仅捕获计数”这个版本很好用…这就是你正在尝试的版本吗?嗨,我按照你的指示做了更改,但是仍然没有改变