“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:0错误

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:0错误,java,Java,我为扫雷舰问题写了一个代码。它创建了一个MxN扫雷游戏,其中每个单元都是概率为p的炸弹。打印出m-by-n游戏和相邻炸弹的数量。 代码: class Minesweeper { public static void main(String[] args) { int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); double p = Double.pa

我为扫雷舰问题写了一个代码。它创建了一个MxN扫雷游戏,其中每个单元都是概率为p的炸弹。打印出m-by-n游戏和相邻炸弹的数量。
代码:

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();
    }

    }
}  

请帮助我。

在解析参数之前,只需给出一个额外的条件

if (args.length >= 3) {
    m = Integer.parseInt(args[0]);
    n = Integer.parseInt(args[1]);
    p = Double.parseDouble(args[2]);
}
可能重复的
if (args.length >= 3) {
    m = Integer.parseInt(args[0]);
    n = Integer.parseInt(args[1]);
    p = Double.parseDouble(args[2]);
}