Tic Tac Toe在Java中打印不正确

时间:2015-05-28 14:53:59

标签: java

我正在用Java创建一个tic-tac-toe游戏,我做的第一步就是制作棋盘。

由于循环,电路板应该是3x3。谁能帮我吗?这是代码:

package myProject;

import java.util.Scanner;

public class TicTacDoe {

    public static int row,col;
    public static Scanner scan = new Scanner(System.in);

    public static char[][] board = new char[3][3];
    public static char turn = 'X';

    public static void main(String[] args) {

        for(int i = 0;i < 3; i++) {
            for(int j = 0;j < 3;j++){
                board[i][j] = '_';
            }
            PrintBoard();
        }

    }

    public static void Play() {

    }

    public static void PrintBoard() {
        for(int i = 0;i < 3; i++) {
            System.out.println();
            for(int j = 0;j < 3;j++) {
                if(j==0)
                    System.out.println("| ");
                System.out.print(board[i][j] + " | ");
            }
        }
    }

    public boolean GameOver(int rMove, int cMove) {
        return false;
    }
}

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

试试这个:

import java.util.Scanner;

public class TicTacToe {

    public static int row,col;
    public static Scanner scan = new Scanner(System.in);

    public static char[][] board = new char[3][3];
    public static char turn = 'X';

    public static void main(String[] args) {

        for(int i = 0;i < 3; i++) {
            for(int j = 0;j < 3;j++){
                board[i][j] = '_';
            }

        }
        PrintBoard();
    }

    public static void Play() {

    }

    public static void PrintBoard() {
        for(int i = 0;i < 3; i++) {
            System.out.println();
            for(int j = 0;j < 3;j++) {
                if(j==0)
                    System.out.print("| ");
                System.out.print(board[i][j] + " | ");
            }
        }
    }

    public boolean GameOver(int rMove, int cMove) {
        return false;
    }
}

将PrintBoard()移出嵌套的for循环,并将if(j == 0)更改为System.out.print

提供输出

| _ | _ | _ | 
| _ | _ | _ | 
| _ | _ | _ |