【问题标题】:C# Creating an Index in a For Loop for an Array [closed]C# 在 For 循环中为数组创建索引 [关闭]
【发布时间】:2021-02-27 06:46:19
【问题描述】:

我正在尝试执行一项任务,并且尝试通过提取我可以从数组中获得的可能性来记录两次掷骰子的结果。基本上,如果我掷出 2、4、5、5 和 2,我会记录我得到两个 2、一个 4 和两个 5。但是,我试图找出记录它的最佳方法,而不必诉诸于列出每个变量 2-12。有人可以帮助我学习如何从我提供的代码中创建快捷方式吗?代码如下:

using System;

namespace Assignment
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize variable(s).
            int diceRollNum = 0;

            //Create the array.
            int[] DiceResultArray = new int[11];

            //Create the random number.
            Random diceRoll = new Random();

            //Write out Headers.
            Console.WriteLine($"Roll\tCount");

            //
            for (diceRollNum = 0; diceRollNum < 36000; diceRollNum++)
            {
                //Roll the dice.
                int firstDice = diceRoll.Next(1, 6);
                int secondDice = diceRoll.Next(1, 6);

                //Add the dice sums.
                diceRollNum = firstDice + secondDice;

                //Record results.
                DiceResultArray[diceRollNum] =
            }

            //
            for (int i = 0; i < DiceResultArray.Length; i++)
            {
                Console.WriteLine($"{i+2}\t{DiceResultArray[i]}");
            }
        }
    }
}

我们正在寻找“记录结果”评论下发生的具体情况。如果有人能帮我解释一下,那就太好了!

【问题讨论】:

  • 你为什么不想这样做var DiceResultArray = new int[12]; DiceResultArray[diceRollNum]++;
  • 这绝对可以工作,尽管执行下面的建议可能更简单。我只需要了解如何为数组创建索引。

标签: c# arrays indexing


【解决方案1】:

您的代码几乎没有问题

  1. diceRollNum 循环更新,无限运行。
  2. Random.Next(minValue, maxValue) 生成一个不包括 maxValue 的随机值。因此,要获得 1 到 6 之间的随机数,我们应该调用 Next(),分别传递 1 和 7 min 和 max 作为参数
  3. 在访问数组时应从diceRollSum(存储骰子值总和的新变量)减少 1,因为数组的索引范围是 0-11,而不是 1-12
using System;

namespace Assignment
{
    class Program
    {
        static void Main( string[] args )
        {
            //Initialize variable(s).
            int diceRollNum = 0;

            //Create the array. 
            int[] DiceResultArray = new int[12];

            //As 1 is not a possible value for the sum of dice values, we can instantiate an array with 11 items and reduce 2 from diceRollSum
            //A slightly optimized Approach noted by Andrew
            //int[] DiceResultArray = new int[11];

            //Creates random instance.
            Random diceRoll = new Random();

            //Write out Headers.
            Console.WriteLine( $"Roll\tCount" );

            //
            for (diceRollNum = 0; diceRollNum < 36000; diceRollNum++)
            {
                //Roll the dice.
                int firstDice = diceRoll.Next( 1, 7 );
                int secondDice = diceRoll.Next( 1, 7 );

                //Add the dice sums.
                int diceRollSum = firstDice + secondDice;

                //Record results.
                DiceResultArray[diceRollSum - 1]++;
                
                //Slightly Optimized
                //DiceResultArray[diceRollSum - 2]++;
            }

            //
            for (int i = 0; i < DiceResultArray.Length; i++)
            {
                Console.WriteLine( $"{i+1}\t{DiceResultArray[i]}" );
                
                //Slightly Optimized
                //Console.WriteLine( $"{i+2}\t{DiceResultArray[i]}" );
            }
        }
    }
}

【讨论】:

  • 2个骰子有11种可能的结果:2到12。如果你想节省一点内存,你当然可以使用new int[11],但接下来需要做diceRollSum - 2(到将 2-12 转换为 0-10)。但我可能更喜欢澄清而不是节省一些内存字节。
  • 是的,感谢@Andrew 通过添加 cmets 更新了解决方案。这样 OP 就可以决定最好的方法:-)
  • 我认为我的目标是清晰,因为代码本身似乎运行良好,但我很感激指出我的代码存在的缺陷。优化绝对是好的,但这里不需要,我认为代码本身的清晰度对于这样的分配更为重要。我认为使用其中任何一个的选择都很棒!简洁的答案绝对是伟大的!谢谢!
猜你喜欢
  • 2015-03-15
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2020-11-20
相关资源
最近更新 更多