gpt4 book ai didi

java - 我在 TicTacToe 游戏中设置 'piece' 的方法不起作用

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

在主方法中调用方法 setPiece(int x, int y, charpiece) 时,我希望棋盘上填充参数中给出的字符,但由于某些原因,我没有得到我所期望的结果当我在调用该方法后打印出电路板时。

public class TicTacToe {
private char[][] board;

public TicTacToe() {
board = new char[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = ' ';
}
}

}

public void setPiece(int x, int y, char piece){
board[x][y] = piece;

}


public String toString(){
String result = "";
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = ' ';
System.out.print(board[r][c] + " | ");
}
System.out.println();

}
return result;
}


public static void main(String[] args) {
TicTacToe board = new TicTacToe();
board.setPiece(1, 2, 'x');
System.out.println(board); //I am expecting here the board to be filled with 'x', but it won't work so.

}

}

我在这里做错了什么?我应该如何重写我的方法以获得我想要的结果?

最佳答案

toString board[r][c] = ' ';错了

public String toString() {
String result = "";
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
System.out.print(board[r][c] + " | ");
}
System.out.println();

}
return result;
}

关于java - 我在 TicTacToe 游戏中设置 'piece' 的方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21728608/

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