[英]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();
}
}
}
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.
我在这里解释另一部分(即以后如何访问它)以及一些有助于您设计应用程序的附加信息。
board[][]
will be changed in fillBoard
, you will get the changed board[][]
in main
.board[][]
将在fillBoard
中更改时,您将在main
中获得更改后的board[][]
。fillBoard
should do only one thing, which is filling up the board.fillBoard
应该只做一件事,就是填满板子。 It should not also do printing (additional thing).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.length
和board[i].length
而不是7
作为行数和7
作为列数,以确保每次更改数组大小时都不必更改它.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.