我如何打印经过修改的 printBoard?

How I can print the printBoard with modification?

我写了一个井字游戏。

这里我用char [][] board创建了一个3x3table,然后调用方法printBoard()。我输入例如 X_X_O____,然后用这些字符打印 table。

changeBoard()方法中,我想输入board[][]的坐标,如果有字符_,这将被替换为X。我在编译时给出坐标,看到坐标是 _ 但是当调用方法 printBoard() 时,控制台打印相同的板,没有任何变化。你能帮帮我吗,因为我不知道我做错了什么?

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();
     }
}

为什么要为 n, m, i, j 使用字段?如果删除它,代码会更干净。

此外,您在 changeBoard 中更改了 board,但随后在行 board[i][j] = state.charAt(nextChar++); 中删除了您的更改。您可以将其从 printBoard 移至构造函数。 我想写这样的东西

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();
    }
}