你好,我是一名初学者程序员,我尝试了各种不同的方法让我的代码工作,但都失败了。如果有人能告诉我我的代码有什么问题,以及如何解决arrayindexoutofboundsexception错误,我将非常感激。非常感谢你提前!为什么我的代码给了我一个ArrayIndexOutOfBoundsException?
这里是我的代码:
public static void main(String[] args) {
// TODO code application logic here
// getting the number of rows and columns for the maze from the user
Scanner scanner = new Scanner(System.in);
System.out.print("How many rows are in the maze? ");
int rows = scanner.nextInt();
int[][] maze = new int[rows][];
System.out.print("How many columns are in the maze? ");
int columns = scanner.nextInt();
maze[rows] = new int[columns];
// getting the data/danger levels for each row from the user
for (int c = -1; c < maze[rows].length; c++) {
System.out.print("Enter the danger in row " + (c + 1) + ", " + "separated by spaces: ");
maze[rows][c] = scanner.nextInt();
}
System.out.println(maze[rows][columns] + "\n");
}
}