这个问题已经在这里有一个答案:
- 什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决? 4个回答
我知道是什么问题,说明的,但我很困惑,我的计划是如何输出值是这样的阵列外..
我有整数是0的数组 - 8,这意味着它可以容纳9个整数,正确的吗? 我有检查,以确保用户的输入值是1-9的INT。 我从整数去除一个(像这样)
if (posStatus[intUsersInput-1] == 0) //if pos is empty
{
posStatus[intUsersInput-1] += 1;
}//set it to 1
然后我输入自己9,我得到的错误。 它应该进入最后的int数组中,所以我不明白为什么我得到一个错误。 相关的代码:
public int[] posStatus;
public UsersInput()
{
this.posStatus = new int[8];
}
int intUsersInput = 0; //this gets try parsed + validated that it's 1-9
if (posStatus[intUsersInput-1] == 0) //if i input 9 it should go to 8?
{
posStatus[intUsersInput-1] += 1; //set it to 1
}
错误:
"Index was outside the bounds of the array." "Index was outside the bounds of the array."