gpt4 book ai didi

java - 带二维数组的 TicTacToe

转载 作者:行者123 更新时间:2023-12-01 06:10:46 25 4
gpt4 key购买 nike

我的教授希望这段代码打印出井字游戏板,但我不完全确定从这里做什么。这是我尝试过的:

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

char[][] board = new char[3][3];
boolean hasWinnerOrStaleMate = false;

while (!hasWinnerOrStaleMate) {
int row;
int col;
boolean setLocation = false;
do {
row = keyboard.nextInt();
col = keyboard.nextInt();

if (board[row][col] == '\u0000') // vacant
{
board[row][col] = 'X';
setLocation = true;
} else {
System.out.println("Occupied Try again");
}
} while (!setLocation);

for (row = 0; row < board.length; row++) {
for (col = 0; col < board.length; col++) {
System.out.print(board[row][col]);
}
}
}

}

最佳答案

这里给你一个提示:当你打印数组时,你应该在打印完一行后打印换行符。

for (row = 0; row < board.length; row++) {
for (col = 0; col < board.length; col++) {
System.out.print(board[row][col]);
}
System.out.println();
}

剩下的部分我留给读者作为练习。

关于java - 带二维数组的 TicTacToe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35320642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com