-1
我寫了一個掃雷問題的代碼。它創建一個MxN掃雷遊戲,其中每個單元格都是概率爲p的炸彈。打印m-n遊戲和相鄰的炸彈計數。 Code:
異常在線程「主」java.lang.ArrayIndexOutOfBoundsException:0錯誤
class Minesweeper {
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
try {
boolean[][] bombs = new boolean[m+2][n+2];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
bombs[i][j] = (Math.random() < p);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (bombs[i][j]) System.out.print("* ");
else System.out.print(". ");
System.out.println();
}
int[][] sol = new int[m+2][n+2];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
// (ii, jj) indexes neighboring cells
for (int ii = i - 1; ii <= i + 1; ii++)
for (int jj = j - 1; jj <= j + 1; jj++)
if (bombs[ii][jj]) sol[i][j]++;
System.out.println();
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (bombs[i][j]) System.out.print("* ");
else System.out.print(sol[i][j] + " ");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我需要解析後,得到的情況?
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
請幫幫我。
的可能的複製[什麼原因導致java.lang.ArrayIndexOutOfBoundsException,如何預防呢?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception -and-怎麼辦-I-防止-IT) – SomeJavaGuy