简体   繁体   中英

Why is the line printed twice?

I created a simple Tic Tac Toe game. Everything runs perfect except that at the end of the game, when the program decides the outcome it prints the last line twice. For example if player 1 wins it prints:

Player 1 has won

Player 1 has won

I've tried to write input.nextLine(); after the user enters the integer but with no luck. Thanks in advance.

Here is my code:

public static void main(String[] args) {

    //The board
    char board[][] = new char [3][3];


    //Players' characters
    char player1 = 'X';
    char player2 = 'O';
    char turn = 0;

    int rowNumber;
    int columnNumber;
    int count = 0;

    //Welcome mesage
    System.out.println("This is a tic tac toe game! \n\n");
    System.out.println("Player1 - X \nPlayer 2 - O \n\n");

    Scanner input = new Scanner(System.in);

    //We display the board
    board(board);
    while (checkWin(board, player1) && checkWin(board, player2)
            && fullBoard(board)) {
        count = count % 2;
        if (count == 0) {
            System.out.println("Player 1 turn! \n");

            turn = player1;
            count++;
        } else if (count == 1) {
            System.out.println("Player 2 turn!");

            turn = player2;
            count++;
        }
        while (true) {
            System.out.println("Enter a row(1-3):");
            rowNumber = input.nextInt();
            System.out.println("Enter a column(1-3):");
            columnNumber = input.nextInt();

            if (board[rowNumber - 1][columnNumber - 1] != 'X'
                    && board[rowNumber - 1][columnNumber - 1] != 'O') {
                board[rowNumber - 1][columnNumber - 1] = turn;
                break;
            } else {
                System.out.println("Place already taken. Enter again!");
            }
        }
        board(board);

        checkWin(board, player1);
        checkWin(board, player2);
        fullBoard(board);
    }
    System.out.println("Just checking;");

}

public static void board(char board[][]) {
    System.out.println(board[0][0] + " | " + board[0][1] + " | "
            + board[0][2]);
    System.out.println(board[1][0] + " | " + board[1][1] + " | "
            + board[1][2]);
    System.out.println(board[2][0] + " | " + board[2][1] + " | "
            + board[2][2]);
}

public static boolean checkWin(char board[][], char player) {
    if (board[0][0] == player && board[0][1] == player && board[0][2] == player || 
            board[1][0] == player && board[1][1] == player && board[1][2] == player || 
            board[2][0] == player && board[2][1] == player && board[2][2] == player || 
            board[0][0] == player && board[1][0] == player && board[2][0] == player || 
            board[0][1] == player && board[1][1] == player && board[2][1] == player || 
            board[0][2] == player && board[1][2] == player && board[2][2] == player || 
            board[0][0] == player && board[1][1] == player && board[2][2] == player || 
            board[2][0] == player && board[1][1] == player && board[0][2] == player){ 
        if(player == 'X'){
            System.out.println("Player 1 has won!");
        }
        else if(player == 'O'){
            System.out.println("Player 2 has won!");
        }
        return false;

    }else {

        return true;
    }

}

public static boolean fullBoard(char board[][]) {
    if ((board[0][0] == 'X' || board[0][0] == 'O')
            && (board[0][1] == 'X' || board[0][1] == 'O')
            && (board[0][2] == 'X' || board[0][2] == 'O')
            && (board[1][0] == 'X' || board[1][0] == 'O')
            && (board[1][1] == 'X' || board[1][1] == 'O')
            && (board[1][2] == 'X' || board[1][2] == 'O')
            && (board[2][0] == 'X' || board[2][0] == 'O')
            && (board[2][1] == 'X' || board[2][1] == 'O')
            && (board[2][2] == 'X' || board[2][2] == 'O')){

        System.out.println("It's a tie!");
        return false;

    }else {

        return true;

    }
}

}

Because you are calling method twice, once in loop condition and second inside loop.

while (checkWin(board, player1) && checkWin(board, player2)
        && fullBoard(board)) {

    ....

    checkWin(board, player1);
    checkWin(board, player2);

}

You don't need to call it inside loop, in condition it is sufficient. Change it to following...

while (checkWin(board, player1) && checkWin(board, player2)
        && fullBoard(board)) {

    ....
    // Remove method call from here.
}

This is due to your while loop here :

while (checkWin(board, player1) && checkWin(board, player2)
        && fullBoard(board)){
    // stuff
    // and then again...
    checkWin(board, player1);
    checkWin(board, player2);
    fullBoard(board);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM