【发布时间】:2016-07-13 07:20:12
【问题描述】:
在这里完成:C # Two-dimensional int array,Sum off all elements,但这次使用的是锯齿状数组。获取:
System.IndexOutOfRangeException。
我是一个寻求帮助的初学者。这是我的代码:
public static int Sum(int[][] arr)
{
int total = 0;
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
total += arr[i][j];
}
}
return total;
}
static void Main(string[] args)
{
int[][] arr = new int[][]
{
new int [] {1},
new int [] {1,3,-5},
};
int total = Sum(arr);
Console.WriteLine();
Console.ReadKey();
}
【问题讨论】:
-
请按照约定规则格式化您的代码。例如在 Visual Studio 中使用 Ctrk+K+D。阅读您的代码真的很不愉快)。阅读更多:msdn.microsoft.com/en-us/library/ff926074.aspx
-
@GiladGreen - 感谢您的回答。程序正在运行。但输出显示空白
-
@Shopska - 您需要将总数添加到
WriteLine -
@GiladGreen 感谢您的好意消息哈哈。请问您,当我添加时,“khlr”答案有什么不同; ' 如果 (arr[i] != null) '?你能解释一下吗?
标签: c# jagged-arrays