Java战舰网格

时间:2016-11-28 03:40:57

标签: java arrays

在这段代码中,我将shipGrid从boolean更改为char。

首先,在shipGrid[i][j] = (Math.random() < chance);if (shipGrid[i][j]) System.out.print("B ");,它表示char无法转换为布尔值。

我知道char不能转换为boolean。此代码的目的是构建一个战舰网格,其中空单元格显示“*”,而带有战列舰的单元格显示字母“B”。

int numColumns;
int numRows;
double chance;
int numGuess;
final char[][] shipGrid = new char[numRows + 2][numColumns + 2];
}

public static void generateShipGrid (char[][] shipGrid, double chance, int seed, int numColumns, int numRows){
    for (int i = 1; i <= numRows; i++) {
        for (int j = 1; j <= numColumns; j++) {
            shipGrid[i][j] = (Math.random() < chance);
        }
    }

    for (int i = 1; i <= numRows; i++) {
        for (int j = 1; j <= numColumns; j++)
            if (shipGrid[i][j]) System.out.print("B ");
            else             System.out.print(". ");
        System.out.println();
        }
}
}

0 个答案:

没有答案