所以基本上我正在尝试为扫雷游戏制作 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/