gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:25:38 25 4
gpt4 key购买 nike

我一直在学习 Java,正在开发一个简单的 Tic-Tac-Toe 游戏。我相信除了 IF 语句未执行之外,一切都正常工作。因此,当玩家连续或对角地获得三个 X 时,IF 语句不会执行。然而,只要玩家打成平局或得到 3 个 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 ");
}

}

最佳答案

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

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

关于java - IF 语句未执行,但 ELSE IF 语句工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26294424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com