小编aki*_*nov的帖子

C#二维int数组,Sum off所有元素

我试图制作一个程序,它总结了数组中的元素.但我在MVS上有' System.IndexOutOfRangeException '错误.谁能说出我的错误在哪里?

public static int Sum(int[,] arr)
{
    int total = 0;
    for (int i = 0; i <= arr.Length; i++)
    {
        for (int j = 0; j <= arr.Length; j++)
        {
            total += arr[i,j];
        }
    }
    return total;
}

static void Main(string[] args)
{
    int[,] arr = { { 1, 3 }, { 0, -11 } };
    int total = Sum(arr);

    Console.WriteLine(total);
    Console.ReadKey(); 
}
Run Code Online (Sandbox Code Playgroud)

c# arrays

2
推荐指数
2
解决办法
98
查看次数

标签 统计

arrays ×1

c# ×1