简体   繁体   English

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

[英]IF statement not executing, but ELSE IF statements are working fine

I've been learning Java and am working on a simple Tic-Tac-Toe game. 我一直在学习Java,并且正在开发一个简单的井字游戏。 I believe I've gotten everything working properly except for the IF statement not executing. 我相信除了IF语句未执行之外,其他所有工作都正常进行。 So when the player gets three X's in a row or diagonally, the IF statement doesn't execute. 因此,当播放器连续或对角获得三个X时,IF语句将不会执行。 However both the ELSE IF statements are working just fine whenever the players tie or get three O's. 但是,每当球员并列或获得三个O时,这两个ELSE IF语句都可以正常工作。 Any help would be much appreciated! 任何帮助将非常感激!

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

Also this is the first time I've posted on Stack Overflow so apologies if the question or formatting is out of place. 这也是我第一次在Stack Overflow上发布文章,如果问题或格式不正确,我们深表歉意。

Full Code: 完整代码:

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

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

您在前两列中使用大X ,在最后一列中使用小x ,这可能是问题的根源

You should take a look at the X s and x s you are using in your code. 你应该看一看在X S和x使用的是在你的代码秒。 Characters are case sensitive. 字符区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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