简体   繁体   English

我如何让这个板像我想要的那样打印出来

[英]How do I get this board to print out like I want it to

Total noob to java and I have a homework where I need to make a custom chess game but I am even struggling with printing the gameboard which I am trying all the empty spaces to be with "X". java 的总菜鸟,我有一个家庭作业,我需要制作一个自定义国际象棋游戏,但我什至在打印游戏板时遇到了困难,我正在尝试将所有空白空间与“X”一起使用。 My current "code":我当前的“代码”:

public class Chess {

    public static void main(String[] args) {
        String[][] board = new String[7][7];
        fillBoard(board);


    }
    public static void fillBoard(String[][] board){
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {
                board[i][j] = "X";
                System.out.print(Arrays.deepToString(board));
            }
            System.out.println();
        }
    }
}

It prints out: This它打印出来:这个

When I want it to print out: Is this当我希望它打印出来时:这是

Just print the value of the position directly.只需直接打印 position 的值即可。

 public static void fillBoard(String[][] board){
            for (int i = 0; i < 7; i++) {
                for (int j = 0; j < 7; j++) {
                    board[i][j] = "X";
                    System.out.print(board[i][j]);
                }
                System.out.println();
            }
        }

After reading your comments, I realized that it's not just about printing the array that you are concerned about;阅读您的评论后,我意识到这不仅仅是打印您关心的数组; rather, you are concerned about how you will be able to access it later.相反,您担心以后如何访问它。 The solution to the problem of incorrect printing has already been provided in the comments ie replacing System.out.print(Arrays.deepToString(board));注释中已经提供了打印不正确问题的解决方案,即替换System.out.print(Arrays.deepToString(board)); with System.out.print("X");System.out.print("X"); . . I am explaining here the other part (ie how to access it later) and also some additional information that will help you in designing your application.我在这里解释另一部分(即以后如何访问它)以及一些有助于您设计应用程序的附加信息。

  1. When you pass and change an array in some method, the array will be changed for the calling method too (check this to learn more about it).当您在某些方法中传递和更改数组时,调用方法的数组也将更改(检查以了解更多信息)。 It means that when board[][] will be changed in fillBoard , you will get the changed board[][] in main .这意味着当board[][]将在fillBoard中更改时,您将在main中获得更改后的board[][]
  2. According to Single-responsibility principle , the module/method, fillBoard should do only one thing, which is filling up the board.根据Single-responsibility 原则,模块/方法, fillBoard应该只做一件事,就是填满板子。 It should not also do printing (additional thing).它也不应该做打印(额外的事情)。 You should write a different method for printing.您应该编写不同的打印方法。
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[][] board = new String[7][7];
        fillBoard(board);
        displayBoard(board);
    }

    static void fillBoard(String[][] board) {
        for (String[] row : board) {
            Arrays.fill(row, "X");
        }
    }

    static void displayBoard(String[][] board) {
        for (String[] row : board) {
            for (String cell : row) {
                System.out.print(cell);
            }
            System.out.println();
        }
    }
}

Output: Output:

XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX

Additional note:附加说明:

A 2-D array is an array of 1-D arrays and therefore there are many ways in which you can write the code given above eg二维数组是一维 arrays 的数组,因此您可以通过多种方式编写上面给出的代码,例如

static void fillBoard(String[][] board) {
    for (int i = 0; i < board.length; i++) {
        Arrays.fill(board[i], "X");
    }
}

or using enhanced for loop:或使用增强for循环:

static void fillBoard(String[][] board) {
    for (String[] row : board) {
        Arrays.fill(row, "X");
    }
}

or without using Arrays.fill :或不使用Arrays.fill

static void fillBoard(String[][] board) {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            board[i][j] = "X";
        }
    }
}

Similarly, the method, displayBoard can be written as:类似地,方法displayBoard可以写成:

static void displayBoard(String[][] board) {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            System.out.print(board[i][j]);
        }
        System.out.println();
    }
}

Note that I have used board.length and board[i].length instead of 7 for the number of rows and 7 for the number of columns to make sure you don't have to change it every time you change the size of the array.请注意,我使用board.lengthboard[i].length而不是7作为行数和7作为列数,以确保每次更改数组大小时都不必更改它.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM