检查矩阵中的特定位置的值并将其与另一个值进行比较。

huangapple go评论73阅读模式
英文:

Check a matrix's value in a certain position and compare it to another

问题

我正在尝试创建一个函数,该函数将以矩阵作为参数,然后使用不同的符号将此数组显示到控制台。为了实现这一目标,我试图检查Matrix[1,1]的值,并比较它以确定需要显示的符号。这一切都是使用C#完成的。

我尝试使用以下代码来读取矩阵并在控制台显示符号:

        // 显示所请求的游戏板
        public static void DisplayBoard(int[,] gameboard)
        {
           int boardLength = gameboard.GetLength(0); // 获取板的大小并存储它
            // 将游戏板显示到控制台上,以正确的符号显示为一个正方形
            for (int i = 1; i <= boardLength; i++)
            {
                for (int j = 1; j <= boardLength; j++)
                {
                    // 检查数组的位置并根据其值更改符号
                    if (gameboard[i, j] == 1) // 玩家一的X
                    {
                        Console.Write("X");
                    }
                    else if (gameboard[i, j] == 2) // 玩家2的O
                    {
                        Console.Write("O");
                    }
                    else // 默认空方块
                    {
                        Console.Write("-");
                    }
                    
                }
                Console.WriteLine(string.Empty);
            }
        }

我期望这段代码会读取矩阵的值并运行for循环,如果矩阵[1,1]中的值等于0,我期望它会进入else语句并将"- "打印到控制台,如果等于1,它会打印"X",等于2会打印"O"。然后它会移动到下一行并重复循环,直到数组被转换为控制台。但实际发生的是,我在第一行的if语句中遇到了System.IndexOutOfRangeException错误,即(if (gameboard[i, j] == 1))。

英文:

Im trying to make a function that will take a matrix as a paramater, then display this array to the console using diffrent symbols. To acomplish this im trying to check the value of Matrix[1,1] and compare it to see what symbol needs to be displayed. This is all using C#.

I tried to use this code to read a matrix and display a symbol to the console

        // Displays the gameboard requested
        public static void DisplayBoard(int[,] gameboard)
        {
           int boardLength = gameboard.GetLength(0); // Gets the size of the board and stores it
            // Displays the game board to the console as a square with correct symbols
            for (int i = 1; i <= boardLength; i++)
            {
                for (int j = 1; j <= boardLength; j++)
                {
                    // Checks the arrays position and changes the symbol based on its value
                    if (gameboard[i, j] == 1) // Player one X
                    {
                        Console.Write("X");
                    }
                    else if (gameboard[i, j] == 2) // Player 2 O
                    {
                        Console.Write("O");
                    }
                    else // default empty square
                    {
                        Console.Write("-");
                    }
                    
                }
                Console.WriteLine(string.Empty);
            }
        }

I Expected this to read the matrixs value and run the for loops, if the value in matrix[1,1] is equal to 0 i expected it to be throw to the else statement and print "-" to the console, is equal to 1 itll print "x" and 2 will be a "o". It would then move to the next row and repeat the loops till the array has been translated to the console.
What happens is im displayed the System.IndexOutOfRangeException: error on the first line with an if statement, if (gameboard[i, j] == 1),

答案1

得分: 0

尝试更改你的for循环。索引从0开始,而不是1。

英文:

Try changing your for loops. The index starts at 0, not 1.

for (int i = 0; i < boardLength; i++)

答案2

得分: 0

这主要是与 C# 处理嵌套数组和与之一起工作的奇特方式有关的问题(索引从 0 开始,长度从 1 开始)。你正在调用包含数组中所有整数的长度,而不管它们嵌套多深,而实际上你应该调用的是 rank,它定义了数组中嵌套数组的数量。请记住,这是一个二维数组,所以我们不能从单一的“长度”来获取正确的结果。

举个例子,当第一个嵌套数组不包含 9 个元素时,你可能会调用 gameboard[1,9],这会导致问题!

你可以通过修改以下语法来修复这个问题,考虑数组的长度和宽度,其中所有嵌套数组都具有相同的大小:

public static void DisplayBoard(int[,] gameboard)
{
   int boardLength = gameboard.Rank - 1;
   int boardWidth = gameboard.GetLength(0) / boardLength;
    for (int i = 0; i <= boardLength; i++)
    {
        for (int j = 0; j <= boardWidth; j++)
        {
            // 检查数组的位置并根据其值更改符号
            if (gameboard[i, j] == 1) // 玩家 1 的 X
            {
                Console.Write("X");
            }
            else if (gameboard[i, j] == 2) // 玩家 2 的 O
            {
                Console.Write("O");
            }
            else // 默认的空方格
            {
                Console.Write("-");
            }
            
        }
        Console.WriteLine(string.Empty);
    }
}

在这里,我们计算了长度和宽度,然后使用这两部分来实际迭代我们的二维数组。

英文:

This is mainly an issue of the way C# works with arrays of nested arrays and the oddities of working with them (index starts at 0, length starts at 1). You're calling the length which contains all ints in the array regardless of how deeply nested they are, whereas you need to be calling the rank which defines the number of nested arrays in your array. Remember this is a 2d array, so we can't count from a single "length" to get it to work.

So for example, you might end up calling gameboard[1,9] when the first nested array doesn't contain 9 elements... and this gets your issue!

You can fix this issue by modifying your syntax as below to consider both length and width of your array where all nested arrays are of the same size:

        public static void DisplayBoard(int[,] gameboard)
        {
           int boardLength = gameboard.Rank - 1;
           int boardWidth = gameboard.GetLength(0) / boardLength;
            for (int i = 0; i &lt;= boardLength; i++)
            {
                for (int j = 0; j &lt;= boardWidth; j++)
                {
                    // Checks the arrays position and changes the symbol based on its value
                    if (gameboard[i, j] == 1) // Player one X
                    {
                        Console.Write(&quot;X&quot;);
                    }
                    else if (gameboard[i, j] == 2) // Player 2 O
                    {
                        Console.Write(&quot;O&quot;);
                    }
                    else // default empty square
                    {
                        Console.Write(&quot;-&quot;);
                    }
                    
                }
                Console.WriteLine(string.Empty);
            }
        }

Here we calculate the length and width and then use these two parts to actually iterate through our 2d array.

huangapple
  • 本文由 发表于 2023年2月14日 03:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定