英文:
How I can print the printBoard with modification?
问题
我写了一个井字游戏。
这里我使用 char [][] board
创建了一个 3x3
的表格,然后调用了 printBoard()
方法。例如,我输入了 X_X_O____
,然后用这些字符打印出这个表格。
在 changeBoard()
方法中,我想要输入 board[][]
的坐标,如果坐标处是字符 _
,就会被替换为 X
。我在编译时提供坐标,以便看到坐标是 _
,但是当调用 printBoard()
方法时,控制台打印出来的仍然是没有任何更改的相同表格。请问您能帮我解决问题吗?
英文:
I write a TicTacToe game.
Here I create a 3x3
table with char [][] board
, then call method printBoard()
. I input for example X_X_O____
, and print this table with these characters.
In the changeBoard()
method I want to input coordinates of board[][]
and if there is char _
this will be replaced with X
. I give the coordinates at compile to see that the coordinate is _
but when call method printBoard()
, the console prints the same board without any changes. Can you please help me as I don't know what I am doing wrong?
import java.util.Scanner;
public class TicTacToe {
private char[][] board = new char[3][3];
private String state;
private int n;
private int m;
private int i;
private int j;
Scanner sc = new Scanner(System.in);
public TicTacToe() {
System.out.print("Enter cells: ");
this.state = sc.nextLine();
}
public void printBoard() {
int nextChar = 0;
System.out.println("---------");
for (i = 0; i < 3; i++) {
System.out.print("| ");
for (j = 0; j < 3; j++) {
board[i][j] = state.charAt(nextChar++);
System.out.print(board[i][j] + " ");
}
System.out.println("|");
}
System.out.println("---------");
}
public void changeBoard() {
while (true) {
System.out.print("Enter the coordinates: ");
n = sc.nextInt();
m = sc.nextInt();
if (n < 1 || n > 3 || m < 1 || m > 3) {
System.out.println("Coordinates should be from 1 to 3!");
} else {
int x = n - 1;
int y = m - 1;
this.i = x;
this.j = y;
if (board[i][j] == '_') {
this.board[i][j] = 'X';
break;
} else {
System.out.println("This cell is occupied! Choose another one!");
}
}
}
// printBoard();
}
}
</details>
# 答案1
**得分**: 1
为什么要使用字段`n, m, i, j`?如果删除它们,代码将更加清晰。
另外,在`changeBoard`中你改变了`board`,但是在`board[i][j] = state.charAt(nextChar++);`这一行中又擦除了你的更改。你可以将这一行从`printBoard`移至构造函数中。我认为你想要写成这样:
```java
class TicTacToe {
private char[][] board = new char[3][3];
Scanner sc = new Scanner(System.in);
public TicTacToe() {
int nextChar = 0;
System.out.print("Enter cells: ");
String state = sc.nextLine();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = state.charAt(nextChar++);
}
}
}
public void printBoard() {
System.out.println("---------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println("|");
}
System.out.println("---------");
}
public void changeBoard() {
while (true) {
System.out.print("Enter the coordinates: ");
int n = sc.nextInt();
int m = sc.nextInt();
if (n < 1 || n > 3 || m < 1 || m > 3) {
System.out.println("Coordinates should be from 1 to 3!");
} else {
int x = n - 1;
int y = m - 1;
if (board[x][y] == '_') {
this.board[x][y] = 'X';
break;
} else {
System.out.println("This cell is occupied! Choose another one!");
}
}
}
printBoard();
}
}
英文:
Why you use fields for your n, m, i, j
? If you remove it, code will be cleaner.
Also, you change your board
in changeBoard
, but then in line board[i][j] = state.charAt(nextChar++);
you erase your changes. You can move this from printBoard
to constructor.
I think want to write something like this
class TicTacToe {
private char[][] board = new char[3][3];
Scanner sc = new Scanner(System.in);
public TicTacToe() {
int nextChar = 0;
System.out.print("Enter cells: ");
String state = sc.nextLine();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = state.charAt(nextChar++);
}
}
}
public void printBoard() {
System.out.println("---------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println("|");
}
System.out.println("---------");
}
public void changeBoard() {
while (true) {
System.out.print("Enter the coordinates: ");
int n = sc.nextInt();
int m = sc.nextInt();
if (n < 1 || n > 3 || m < 1 || m > 3) {
System.out.println("Coordinates should be from 1 to 3!");
} else {
int x = n - 1;
int y = m - 1;
if (board[x][y] == '_') {
this.board[x][y] = 'X';
break;
} else {
System.out.println("This cell is occupied! Choose another one!");
}
}
}
printBoard();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论