Java 第一行上的2D阵列打印
出于某种原因,它在一行上打印了九次“*”。而不是3x3的* 还应注意,main函数只是运行displayBoard函数,不会更改输出Java 第一行上的2D阵列打印,java,Java,出于某种原因,它在一行上打印了九次“*”。而不是3x3的* 还应注意,main函数只是运行displayBoard函数,不会更改输出 public class TicTacToeTwoPlayer { static final int SIZE = 3; /* Here: Declare a 2-d character array called board with SIZE rows and SIZE columns. Be sure to
public class TicTacToeTwoPlayer {
static final int SIZE = 3;
/*
Here:
Declare a 2-d character array called board with SIZE rows and SIZE columns.
Be sure to use the static modifier in the declaration
*/
static char[][] board = new char [SIZE][SIZE];
static final char PLAYER1 = 'X';
static final char PLAYER2 = 'O';
static Scanner userInput = new Scanner(System.in);
public static void initializeBoard() {
/*
Here:
Initialize each position of the board array to a space character.
*/
for (int row = 0; row < board.length; row++) {
for(int col = 0; col < board.length; col++) board[row][col] = ' ';
}
}
public static void displayBoard() {
/*
Here:
Complete this method so that it displays the tic-tac-toe board
to the screen. If a board position is available, that is, if it is
a space, output an asterisk followed by a space ("* ".)
*/
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == ' ') {
System.out.print("* ");
} else {
System.out.print(board[row][col]);
}
}
}
System.out.println("Board is: ");
}
}
公共类TictoeTowPlayer{
静态最终整数大小=3;
/*
在这里:
声明一个名为board的二维字符数组,其中包含大小行和大小列。
请确保在声明中使用静态修饰符
*/
静态字符[][]板=新字符[SIZE][SIZE];
静态最终字符播放器1='X';
静态最终字符播放器2='O';
静态扫描仪用户输入=新扫描仪(System.in);
公共静态无效初始化板(){
/*
在这里:
将电路板阵列的每个位置初始化为空格字符。
*/
对于(int row=0;row
任何帮助都将不胜感激 尝试在每行之后打印换行符:
公共静态无效显示板(){
对于(int row=0;row
我不需要这样做,对吗?它是一个二维阵列。这可能行得通,但我相信有些事情根本就错了。我不需要纠正?
。。。你为什么这么想<代码>打印不会自动打印换行符。因此,您需要在每一行之后显式地打印一个CR?LF,或者像我上面所做的那样调用println
。我看到一些2D数组的示例时觉得这是不必要的。我认为这是因为我看到的大多数示例都是在创建数组时手动格式化的字符串数组。