【发布时间】:2020-03-10 07:10:35
【问题描述】:
代码一直运行到第二个 for 循环中的 if 语句。我试过改变很多东西——添加了第二个数组,这样它就不会与 if 语句冲突。对其进行了调试并更改了最后一个 if 的真实语句,但它从未真正通过第 23 行,它显示 System.IndexOutOfRangeException: Index was outside the bounds of the array。
Console.WriteLine("Number of inputs: ");
int numInput = int.Parse(Console.ReadLine());
int[] arrayOfNumbers = new int[numInput];
int[] arrayOfNumbersClone = new int[numInput];
for (int inc = 0; inc < arrayOfNumbers.Length; inc++)
{
Console.Write("Enter {0} element: ", inc + 1);
arrayOfNumbers[inc] = Int32.Parse(Console.ReadLine());
arrayOfNumbersClone[inc] = arrayOfNumbers[inc];
}
for (int inc = 0, dec = numInput; inc2 < dec; inc2++, dec--)
{
if (arrayOfNumbers[inc] == arrayOfNumbersClone[dec])
{
counter++;
}
else
{
}
}
if(counter<=0)Console.WriteLine("The array is not symmetric");
else Console.WriteLine("The array is symmetric");
【问题讨论】:
-
您知道,在第二个循环中,您初始化“inc”,但测试并增加“inc2”,在“if”中,您使用从未修改过的“inc”?
-
你必须初始化
dec = numInput - 1
标签: c# arrays for-loop if-statement indexoutofboundsexception