编码初学者,我需要帮助 [英] Beginner at Coding , I Need Help
问题描述
我不知道为什么不能将else语句中的索引分配给整数数组.
I don't know why I can't assign the index in my else statement to my integer array.
每次我的程序转到else语句时,它都会中断,并显示System.Index.OutOfRangeException.
Everytime my program goes to the else statement it breaks saying System.Index.OutOfRangeException.
有什么建议吗?
推荐答案
声明数组时,必须说它的大小(可以容纳多少个元素,哪个范围从0到size-1)
When you declare an array, you have to say how big it is (how many elements it can hold, which will range from 0 to size-1)
您要声明输出数组的大小为零!
You are declaring the output array with a zero size!
int indexTwo = 0;
int[] output = new int[indexTwo]; // zero-element array!
因此,任何设置数组元素的尝试都将导致OutOfRangeException.
So any attempt to set an element of the array will result in an OutOfRangeException.
查看您的代码,索引可以从0循环到maxsize-1(maxsize元素).因此output []至少必须那么大.将声明更改为:
Looking at your code, index can loop from 0 to maxsize-1 (maxsize elements). And so output[] must be at least that big. Change your declaration to:
int[] output = new int[maxsize];
这篇关于编码初学者,我需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!