内索引数错误
using System;
class Test
{
public static void Main()
{
int[,] a= new int[5,5];
a[0,0]=1;
for(int i=1;i <=5;i++)
{
a[i,0]=1;
a[i,i]=1;
for(int j=1;j <i;j++)
{
a[i,j]=a[i-1,j-1]+a[i-1,j];
}
}
for(int i=0;i <5;i++)
{
for(int j=0;j < i;j++){
Console.WriteLine( "{0} ",a[i][j]);
}
Console.WriteLine();
}
}
}
打印杨辉三角的例子
但调试时提示:E:\mydoc\Visual Studio Projects\ConsoleApplication5\Class1.cs(20): [] 内索引数错误,应为“2”
是什么原因?应该如何修改?内索引数错误一共有几种情况呢?
------解决方案--------------------using System;
class Test
{
public static void Main()
{
int[,] a = new int[5, 5];
a[0, 0] = 1;
for (int i = 1; i < 5; i++)
{
a[i, 0] = 1;
a[i, i] = 1;
for (int j = 1; j < i; j++)
{
a[i, j] = a[i - 1, j - 1] + a[i - 1, j];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write( "{0} ", a[i,j]);
}
Console.WriteLine();
}
}
}