c#初心者 見たことないエラ〜が出る 助けて
フィボナッチ数列の第20項までを、配列を使って出力しようとしたところ、以下のようなエラーが出ました。
indexと書いてあるので、forのところでiのはんいをミスったかなと思ったのですがそんな様子がなく困ってます。
有識者の方教えてください
###エラー
Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at test.Main () [0x00017] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at test.Main () [0x00017] in :0
該当するソースコード
using System;
class test
{
static void Main()
{
int[] a = new int[20];
a[0] = 1;
a[1] = 1;
for(int i=2; i<=a.Length; ++i)
{
a[i] = a[i-1] + a[i-2];
Console.WriteLine(a[i]);
}
}
}
0 likes