小编awe*_*awe的帖子

array[i] = java.lang.IndexOutOfBoundsException:无效的数组范围 10 到 10

我的问题是我在调试过程中不断收到标题中的错误。

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10
Run Code Online (Sandbox Code Playgroud)

以下是for循环。我输入了 values people = 10, payer = 4, rest = 6

int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;

for(int i=0; i<people; i++) {
    if(payer<=0) { /*Payers are already decided*/
        array[i] = 0;
        continue;
    }
    if(rest<=0) {
        array[i] = 1;
        continue;
    }
    else {
        int randomNumber = rand.nextInt(2);
        array[i] = randomNumber;
        if (randomNumber == 1)
            payer--;
        if (randomNumber == 0)
            rest--;
    } …
Run Code Online (Sandbox Code Playgroud)

java arrays random android indexoutofboundsexception

5
推荐指数
1
解决办法
3688
查看次数