「インデックスは配列の境界外です」というエラーが表示されます。このコードはテキストブックから入力したものです。カントはそれに何か間違っているようだ。配列の境界外のC#
class Program
{
static void Main()
{
int[] scores = new int[8];
int x;
string inputString;
for (x = 0; x < scores.Length; ++x)
{
Write("Enter your score on test {0} ", x + 1);
inputString = ReadLine();
scores[x] = Convert.ToInt32(inputString);
}
WriteLine("\n----------------------------");
WriteLine("Scores in original order: ");
for(x = 0; x < scores.Length; ++x)
Write("{0, 6}", scores[x]);
WriteLine("\n----------------------------");
Array.Sort(scores);
WriteLine("Scores in sorted order: ");
for(x = 0; x < scores.Length; ++x)
Write("{0, 6}", scores[x]);
WriteLine("\n----------------------------");
Array.Reverse(scores);
WriteLine("Scores in reverse order: ");
for(x = 0; x < scores.Length; ++x) ;
Write("{0, 6}", scores[x]);
}
}
}
デバッガで1行ずつステップ実行します。デバッガの使い方がわからない場合は、それを読んでください。その1つのツールは無駄な時間を無駄にすることなく時間を節約し、プログラムの流れをよく理解するのに役立ちます。 –
'++ x'の代わりに' x ++ 'を試してください – Phaeze
@Phaezeそれはどうでもいいのですか?単一の変数だけの式の場合、接頭辞vs接尾辞は関係ありません。 – Adrian