IF语句未执行,但ELSE IF语句正常工作

时间:2014-10-10 07:50:49

标签: java

我一直在学习Java,正在开发一款简单的Tic-Tac-Toe游戏。我相信我已经完成了一切正常工作,除了IF语句没有执行。因此,当玩家连续获得三个X或对角线时,IF语句不会执行。然而,无论何时球员并列或获得三个O,ELSE IF声明都能正常工作。任何帮助将不胜感激!

public static void check_status()
{
    if (    (board[0][0]=='X' && board[0][1]=='X' && board[0][2]=='x') ||
            (board[1][0]=='X' && board[1][1]=='X' && board[1][2]=='x') ||
            (board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='x') ||

            (board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='x') ||
            (board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='x') ||
            (board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='x') ||

            (board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='x') ||
            (board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='x') )
    {
        System.out.println();
        System.out.println("X wins the game!");
        end_game = 1;
    }

    else if (    (board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O') ||
            (board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O') ||
            (board[2][0]=='O' && board[2][1]=='O' && board[2][2]=='O') ||

            (board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O') ||
            (board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O') ||
            (board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O') ||

            (board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') ||
            (board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O') )
    {
        System.out.println();
        System.out.println("O wins the game!");
        end_game = 1;
    }


    else if (   (board[0][0]!=' ' && board[0][1]!=' ' && board[0][2]!=' ') &&
                (board[1][0]!=' ' && board[1][1]!=' ' && board[1][2]!=' ') &&
                (board[2][0]!=' ' && board[2][1]!=' ' && board[2][2]!=' ') )
    {
        System.out.println();
        System.out.println("The game is a tie.");
        end_game = 1;
    }
}

这也是我第一次在Stack Overflow上发布,所以如果问题或格式不合适就道歉。

完整代码:

import java.util.Scanner;

public class TicTacToe
{

    private static char[][] board = new char[3][3];

    private static int end_game = 0;

    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);

        initBoard();
        displayBoard();

        do
        {

            System.out.println();
            System.out.print("\'O\', choose your location (row, column): ");
            int O_row = keyboard.nextInt();
            int O_col = keyboard.nextInt();
            board[O_row][O_col] = 'O';
            System.out.println();
            displayBoard();
            check_status();
            if (end_game==1){
                break;
            }
            System.out.println();
            System.out.print("\'X\', choose your location (row, column): ");
            int X_row = keyboard.nextInt();
            int X_col = keyboard.nextInt();
            board[X_row][X_col] = 'X';
            System.out.println();
            displayBoard();
            check_status();

        } while (end_game==0);
    }

    public static void check_status()
    {
        if (    (board[0][0]=='X' && board[0][1]=='X' && board[0][2]=='x') ||
                (board[1][0]=='X' && board[1][1]=='X' && board[1][2]=='x') ||
                (board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='x') ||

                (board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='x') ||
                (board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='x') ||
                (board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='x') ||

                (board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='x') ||
                (board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='x') )
        {
            System.out.println();
            System.out.println("X wins the game!");
            end_game = 1;
        }

        else if (    (board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O') ||
                (board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O') ||
                (board[2][0]=='O' && board[2][1]=='O' && board[2][2]=='O') ||

                (board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O') ||
                (board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O') ||
                (board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O') ||

                (board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') ||
                (board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O') )
        {
            System.out.println();
            System.out.println("O wins the game!");
            end_game = 1;
        }


        else if (   (board[0][0]!=' ' && board[0][1]!=' ' && board[0][2]!=' ') &&
                    (board[1][0]!=' ' && board[1][1]!=' ' && board[1][2]!=' ') &&
                    (board[2][0]!=' ' && board[2][1]!=' ' && board[2][2]!=' ') )
        {
            System.out.println();
            System.out.println("The game is a tie.");
            end_game = 1;
        }
    }

    public static void initBoard()
    {
        // fills up the board with blanks
        for ( int r=0; r<3; r++ )
            for ( int c=0; c<3; c++ )
                board[r][c] = ' ';
    }


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

}

2 个答案:

答案 0 :(得分:4)

board[0][2]=='x' 

你在两个第一列中使用了大X,在最后一列中使用了小x,这可能是问题的根源

答案 1 :(得分:0)

您应该查看代码中使用的Xx。字符区分大小写。