ユーザーが編集できる動的な 2D 配列を作成する必要があります。私はさまざまな方法を試しましたが、診断を容易にするために個別に実行しようとしましたが、常にjava.lang.ArrayIndexOutOfBoundsException
. 以下は、問題を示すコード (私のプロジェクトからのものではない) です。ボードを で埋めようとすると、エラーが発生し0
ます。
public class Example {
public static void main (String args[]) {
int rows = 0;
int cols = 0;
int[][] board = new int[rows][cols];
Scanner scan = new Scanner (System.in);
System.out.print("Enter in a row :");
rows = scan.nextInt();
System.out.print("Enter in a col :");
cols =scan.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
board[i][j] = 0;
System.out.print ("\t" + board[i][j]);
}
System.out.print ("\n");
}
}
}