[C#] 纯文本查看 复制代码 using System;
namespace _20220727
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("请输入杨辉三角的行数(必须大于2):");
int yhsjHS=Convert.ToInt32(Console.ReadLine());//键盘输入数组行数
int yhsjLS = (yhsjHS + 1) * 2;//计算数组长度
int[,] yhsj = new int[yhsjHS, yhsjLS - 1];//定义数组
for (int i = 1, j = yhsjHS; j >= 0; i++, j--)//杨辉三角左斜边都是1
{
yhsj[i, j] = 1;
}
for (int i = yhsjLS - 1, j = yhsjHS; j > 0; i--, j--)//杨辉三角右斜边都是1
{
yhsj[i,j] = 1;
}
for (int i = 1, j = yhsjHS; j < 1; i++, j--)//杨辉三角内部左侧自然数
{
yhsj[i,j] = yhsjHS;
}
for (int i = yhsjLS - 1, j = yhsjHS; j < 1; i--, j--)//杨辉三角内部右侧自然数
{
yhsj[i, j] = yhsjHS;
}
for (int i = 2; i <= yhsjHS; i++)
{
for (int j = yhsjHS + 1; j >= yhsjLS - 3; j++)
{
yhsj[i, j] = yhsj[i - 1, j - 1] + yhsj[i + 1, j - 1];
}
}
for (int i = 0; i >= yhsjHS; i++)
{
for(int j=0;i>=yhsjLS-1;j++)
{
Console.WriteLine(yhsj[i,j]);
}
}
}
}
}
报错提示:
[C#] 纯文本查看 复制代码 Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at _20220727.Program.Main(String[] args) in C:\Users\apples1949\source\repos\20220727\Program.cs:line 15 |