System.Indexoutofrangeexception 错误无法理解

System.Indexoutofrangeexception error not understood

你好我是c#初学者,我不明白为什么在下面显示的程序中抛出异常

计划:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, i, x=0, y=0;
            Console.WriteLine("Enter the degree of matrix:");
            n = int.Parse(Console.ReadLine());
            int[,] num = new int[n, n];
            int p = n * n;
            for (i = 1; i <= p; i++)
            {
                num[x, y] = i;
                if (num[x, y] % n == 0) { y++; }
                if (y % 2 == 0) { x++; }
                if (y % 2 != 0) { x--; }
            }
            for (i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    Console.Write(num[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

要求的结果:

Enter order of matrix:
4

1 8 9  16
2 7 10 15
3 6 11 14 
4 5 12 13

但是在num[x ,y]=i处抛出了题目中所述的异常; .我不明白为什么 System.IndexOutOfRangeException 会出现,因为循环显然在二维数组的末尾结束。

P.S。该程序只用于 运行 一次。

也许我错了,但如果你插入 2 作为矩阵的阶数,变量 p 等于 4。

外部for循环

for (i = 1; i <= p; i++)

循环 4 次,从 1 到 4。但是当 i == 4 时,X 等于 -1,并且您不能访问具有负索引的矩阵。你会得到与 degree = 4 相同的行为,就像你的例子一样。

您不得在更改 y 值的同一循环迭代中更改 x 值,因为这样做不会填充第二列中的最后一个位置并随后进行迭代下降到 x = -1 这是一个无效索引。

    for (i = 1; i <= p; i++)
    {
        num[x, y] = i;
        if (i % n == 0) { 
            y++; 
        }
        else {
            if (y % 2 == 0) { x++; }
            if (y % 2 != 0) { x--; }
        }
    }
    for (i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            Console.Write(num[i, j] + " ");
        }
        Console.WriteLine();
    }

你可以试试这个:你犯了一个基本错误,这里 x 在 y 递增时递减,在递增 y 后使用 continue 语句跳过循环,我保证这个工作 100%。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Whosebug
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, i, x = 0, y = 0;
            Console.WriteLine("Enter the degree of matrix:");
            n = int.Parse(Console.ReadLine());
            int[,] num = new int[n, n];
            int p = n * n;

            for (i = 1; i <= p; i++)
            {
                try
                {
                    num[x, y] = i;
                    if (num[x, y] % n == 0) { y++; continue; }
                    if (y % 2 == 0) { x++; }
                    if (y % 2 != 0) { x--; }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            for (i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(num[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}