我在打井字问题时遇到了一些麻烦。我检查了我的代码很多次,但是找不到问题。我仔细检查了所有的括号和括号。当我在命令行中运行它时,它表示存在表达式和';'的非法使用。预期。我想知道是否可以发现问题。
输入:
import java.util.Scanner;
public class TicTacToe
{
public static int row, col;
public static Scanner scan = new Scanner(System.in);
public static char[][] board = new char[3][3];
public static char playerTurn = 'X';
public static void main(String[] args)
{
// Implement play method
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;
System.out.println("Player X's turn to play! ");
// Implement print board method
while(playing)
{
System.out.println("\nEnter row and column ");
row = scan.nextInt()-1;
col = scan.nextInt()-1;
board[row][col]=playerTurn;
if(GameOver(row,col))
{
playing = false;
System.out.println("Game over! player" + playerTurn + " wins! ");
}
PrintBoard();
if(playetTurn == 'X')
playerTurn = 'O';
else
playerTurn = 'X';
if(playerTurn == 'X')
{
System.out.println("Player X's turn to play! ");
}
else if(playerTurn == 'O')
{
System.out.println("Player O's turn to play! ");
}
}
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();
}
// Check if game is over
public static boolean GameOver(int move1,int move2)
{
if(board[0][move2] == board[1][move2] && board[0][move2] == board[2][move2])
return true;
if(board[move1][0] == board[move1][1] && board[move1][0] == board[move1][2])
return true;
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;
}
}
}