Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2D阵列网格_Java_Grid_Multidimensional Array - Fatal编程技术网

Java 2D阵列网格

Java 2D阵列网格,java,grid,multidimensional-array,Java,Grid,Multidimensional Array,所以我基本上是在为扫雷游戏制作一个9x9网格。我需要用问号填充网格,以表示尚未选定的雷区Ex:[?][?][?][?][?]基本上,我的问题是如何让我的程序输出一系列这样的问号 import java.util.Scanner; import java.util.Arrays; public class H4_Minesweeper { public static void main(String[] args) { //Game Description and ru

所以我基本上是在为扫雷游戏制作一个9x9网格。我需要用问号填充网格,以表示尚未选定的雷区Ex:[?][?][?][?][?]基本上,我的问题是如何让我的程序输出一系列这样的问号

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++){

            }
        }
    }
}     
import java.util.Scanner;
导入java.util.array;
公共级H4_扫雷艇{
公共静态void main(字符串[]args){
//游戏描述和规则
“扫雷舰是一个非常简单的游戏,规则很简单。”;
System.out.println(“打开一个地雷(x),游戏结束。打开一个空方块(o),你继续玩。”);
System.out.println(“问号(?)将表示您尚未揭开的瓷砖。”);
println(“揭开一个数字,它会告诉你周围八个方格中隐藏了多少地雷。”);
System.out.println(“使用此信息仔细选择要单击的方块”);
System.out.println(“\n\n\n”);
扫描仪用户输入;
字符串[][]板=新字符串[9][9];
对于(int r=0;r这应该可以做到

for (int r = 0; r<board.length;r++){
   for (int c = 0; c <board.length;c++){
      board[r][c] = "?";  // Initialize the cell
      System.out.print("[" +board[r][c] + "]"); // Display the content of cell board
   }
   System.out.println();  // go to next line
}

对于(int r=0;r首先,必须通过将数组的所有元素设置为
“?”
来初始化数组:

String[]board=新字符串[9][9];

对于(int r=0;r用字符串“?”为每个网格空间填充2d数组,然后逐行打印每个数组索引的值

填充阵列:

String[][] board = new String[9][9];

for(int y=0;y<9;y++){
    for(int x=0;x<9;x++){
        board[x][y] = "?";
    }
}
String[]board=新字符串[9][9];

对于(int y=0;y你的2d数组已经填充了吗?你有什么理由希望得到预期的输出吗?它总是5吗?不,对不起,我应该提到这一点。我对数组是完全陌生的,我很难弄清楚它的基础。我需要问号来表示9x9网格中未选择的字段。在我有了这些问号之后,我将继续t用户输入以选择他们想要发现的字段。我最初的问题中的五个只是一个示例,以了解网格将开始是什么样子。我想你不应该重新声明
board
以进行打印:D。@Tom true。复制粘贴错误:-)您可以使用
StringBuilder
来避免连接。@Tom我为什么要避免连接…使用StringBuilder无论如何都不能提高运行时速度…而且这更容易理解,而且打字更少
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();
}
String[][] board = new String[9][9];

for(int y=0;y<9;y++){
    for(int x=0;x<9;x++){
        board[x][y] = "?";
    }
}
for (int r = 0; r<9;r++){
    String line = "";
    for (int c = 0; c <9;c++){
        line+="["+board[c][r]+"]";
    }
    System.out.println(line);
}