简体   繁体   English

C# if 语句和 2 arrays 问题

[英]C# Issues with if-statement and 2 arrays

The code runs up until the if statement in the second for-loop.代码一直运行到第二个 for 循环中的 if 语句。 Ive tried changing quite a lot of stuff -- added a second array so it doesn't conflict with the if statement.我尝试过改变很多东西——添加了第二个数组,这样它就不会与 if 语句冲突。 Debugged it and also changed the true statements of the last if but it never really passes through the 23 row and it shows System.IndexOutOfRangeException: Index was outside the bounds of the array.对其进行了调试并更改了最后一个 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");

i think it is because you used inc2 in the for loop condition but never really assigned any value to it我认为这是因为您在 for 循环条件中使用了inc2但从未真正为其分配任何值

change your code to将您的代码更改为

for (int inc2 = 0, dec = numInput; inc2 < dec; inc2++, dec--)

The error says that you trying to get index which is not existed in array.该错误表示您试图获取数组中不存在的索引。 So just add check conditions:所以只需添加检查条件:

int counter = 0;
int lengthNumbers = arrayOfNumbers.Length;
int lengthNumbersClone = arrayOfNumbersClone.Length;            
for (int inc2 = 0, dec = numInput; maxInc < dec; inc2++, dec--)
{
    if (inc2 < lengthNumbers
        && dec < lengthNumbersClone
        && arrayOfNumbers[inc2] == arrayOfNumbersClone[dec])
        {
           counter++;
        }
        else
        {                }

}

Let's say that numInput = 5.假设 numInput = 5。

Then you'll create an array with 5 elements.然后,您将创建一个包含 5 个元素的数组。 The 5th element has the index of 4 since the index starts counting at 0.第 5 个元素的索引为 4,因为索引从 0 开始计数。

In the second loop you declare dec = numInput;在第二个循环中声明 dec = numInput; Therefore, dec is now also 5.因此,dec 现在也是 5。

In your if statement you request arrayOfNumbersClone[dec].在您的 if 语句中,您请求 arrayOfNumbersClone[dec]。 Since dec is 5, you're asking for the element on the 5th index.由于 dec 为 5,因此您要求第 5 个索引上的元素。 This is the 6th item, which doesn't exist.这是第 6 项,不存在。 Therefore you get the "System.IndexOutOfRangeException"因此你得到“System.IndexOutOfRangeException”

Changing your second for-loop to the following should fix your problems将您的第二个 for 循环更改为以下内容应该可以解决您的问题

for (int inc = 0, dec = numInput - 1; inc < dec; inc++, dec--)

(Also note that the undefined 'inc2' is changed to 'inc') (另请注意,未定义的 'inc2' 更改为 'inc')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM