基本上,我得到了第19行的一个ArrayOutOfBounds异常。我试图在不分配任何值的情况下裁剪数组的内容(注释掉了wile循环),但仍然出现相同的错误,任何人有修复?数组越界异常,找不到问题
package com.company;
import java.util.Random;
public class Main {
static int[][] Placer = new int[2][2]; //create multi-dimenstional array to hold the numbers
public static void main(String[] args) {
Random random = new Random(); //intiallise new Random object
int intsAssigned = 0; //will keep track of the ammount of ints assigned.
//Assign all of the index's a random int, not the most practical way.
while(intsAssigned != 9){
int currentColumn = 0;
Placer[currentColumn][0] = random.nextInt(20);
Placer[currentColumn][1] = random.nextInt(20);
Placer[currentColumn][2] = random.nextInt(20);
currentColumn++;
intsAssigned + 3;
}
printNumbers();
}
//Print all of the index's
public static void printNumbers(){
int numbersPrinted = 0;
int currentColumn = 0;
int currentRow = 0;
while(numbersPrinted != 9){
System.out.println(Placer[currentColumn][currentRow]);
System.out.println(Placer[currentColumn][++currentRow]);
System.out.println(Placer[currentColumn][currentRow + 2]);
currentColumn++;
}
}
}
是的,刚刚看到了。谢谢,现在编辑。还有什么区别? –
你也似乎从不增加数字打印,所以虽然不会退出。相反,currentColumn将继续向上,直到超出范围 – StephenTG
此外,currentColumn在循环内被初始化为0,所以无论什么时候使用,它都不会是0但是不是0 – StephenTG