Java 2D 数组网格

标签 java grid multidimensional-array

所以基本上我正在尝试为扫雷游戏制作 9x9 网格。我需要网格中填满问号来代表尚未选择的雷区。 例如:[?][?][?][?][?]基本上我的问题是如何让我的程序输出这样的问号数组?

import java.util.Scanner;
import java.util.Arrays;

public class H4_Minesweeper {

    public static void main(String[] args) {
        //Game Description and rules
        System.out.println("Minesweeper is a very straightforward game, the rules are simple.");
        System.out.println("Uncover a mine (x), and the game ends. Uncover an empty square (o), and you keep playing.");
        System.out.println("A question mark (?) will represent tiles you have not uncovered yet.");
        System.out.println("Uncover a number, and it tells you how many mines lay hidden in the eight surrounding squares.");
        System.out.println("Use this information to carefully choose which squares to click.");
        System.out.println("\n\n\n");

        Scanner userin;


        String[][] board = new String [9][9];
        for (int r = 0; r<board.length;r++){
            for (int c = 0; c <board.length;c++){

            }
        }
    }
}     

最佳答案

首先,您必须通过将数组的所有元素设置为 "?" 来初始化数组。 :

String[][] board = new String [9][9];
for (int r = 0; r<board.length;r++){
    for (int c = 0; c <board.length;c++){
        board[r][c] = "?";
    }
}

然后你可以打印它:

for (int r = 0; r<board.length;r++){
    for (int c = 0; c <board.length;c++){
        System.out.print (board[r][c] + " ");
    }
    System.out.println();
}

关于Java 2D 数组网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26327579/

相关文章:

css - 元素在右下角向左浮动

python - Numpy:对行求和

javascript - 从一个表到另一个表的 Angular 拖放

java - DateFormat.format 返回 01 :00:00 with HH:mm:ss format

java - getClass 返回什么类型?

java - 用于稀疏矩阵的 UJMP Java 库

javascript - 如何将空行添加到 ExtJS 网格?

excel - ComponentArt:导出网格数据

javascript - 在本地存储中创建多维数组时出现问题

java - 更新java时可以自动更新JRE_HOME吗?