gpt4 book ai didi

Java 不可达语句 Tic Tac Toe

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

import java.util.Scanner;

public class TTT{
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] = '_';
}
}

Play();


}
public static void Play(){
boolean playing = true;
printBoard();
while(playing){
System.out.println("Enter a row(1,2,3): ");
row = scan.nextInt() - 1;
System.out.println("Enter a column(1,2,3): ");
col = scan.nextInt() - 1;
board[row][col] = turn;
if (GameOver(row, col)){
playing = false;
System.out.println("Game Over! Player " + turn + " wins!");

}
if (turn == 'X')
turn = 'O';
else
turn = 'X';


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

}
}
System.out.println();

}
public static boolean GameOver(int xToken,int oToken){
// Check perpendicular victory
if (board[0][oToken] == board[1][oToken]
&& board[0][oToken] == board[2][oToken])
return true;
if (board[xToken][0] == board[xToken][1]
&& board[xToken][0] == board[xToken][2])
return true;
// Check diagonal victory
if (board[0][0] == board[1][1] && board[0][0] == board[2][2]
&& board[1][1] != '_')
return true;
if (board[0][2] == board[1][1] && board[0][2] == board [2][0]
&& board[1][1] != '_');
return true;
return false;

}
}

我正在尝试完成我的井字游戏板,但是我在最终返回时收到一条无法访问的声明 false;陈述。我也无法让我的程序经历整个 for 循环。请帮忙!

最佳答案

看看你的if条件

if (board[0][2] == board[1][1] && board[0][2] == board [2][0]
&& board[1][1] != '_');<--

您以 ; 结束您的声明因此最后 2 次返回成为必然结果。

如果你删除它;那你就没事了。

这就是我成为 {} 粉丝的原因。良好的编码实践是始终使用 {} 来表示 if else。

关于Java 不可达语句 Tic Tac Toe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686598/

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