Java 我的Tictatcoe代码有什么问题?您如何检查已拍摄的照片?

Java 我的Tictatcoe代码有什么问题?您如何检查已拍摄的照片?,java,input,Java,Input,我已经编辑了这个问题。我觉得我很接近了,但出于某种原因,该代码的输出会在电路板上方额外打印一个“X”或“O”。我无法精确指出它为什么这样做,以及如何检查是否使用了一个点,并且不允许覆盖该点。我尝试过放置一个if(board[I][j]!=''{S.O.P(“无效”)},但它不能正常工作。也许我写错地方了 最后一件事是,当我想重新开始游戏时,已经宣布了赢家。如何让游戏重置棋盘 我已经做了几个小时了,非常感谢你的帮助 import java.util.Scanner; public class T

我已经编辑了这个问题。我觉得我很接近了,但出于某种原因,该代码的输出会在电路板上方额外打印一个“X”或“O”。我无法精确指出它为什么这样做,以及如何检查是否使用了一个点,并且不允许覆盖该点。我尝试过放置一个if(board[I][j]!=''{S.O.P(“无效”)},但它不能正常工作。也许我写错地方了

最后一件事是,当我想重新开始游戏时,已经宣布了赢家。如何让游戏重置棋盘

我已经做了几个小时了,非常感谢你的帮助

import java.util.Scanner;

public class TicTacToe
{
public static char[][] board = new char[3][3];

public static char player;
public static void newGame()
{
    System.out.println("New Game: X goes first.");
    player = 'X';
}

public static void writeBoard()
{
    //drawing the game board
    String line = "______";
    System.out.println("");
    //making seperate columns and rows
    for(int i= 0;i<3; i++)
    {
        for(int j=0;j<3;j++)
        System.out.print(board[i][j]+"|");
        System.out.println("");
        System.out.println(line);
    }
}

public static void getMove()
{
    Scanner keyboard = new Scanner(System.in);
    int i, j;
    if(player == 'X')
    {
        System.out.println("X's Turn");
        System.out.println("Where do you want your X placed?");
        System.out.println("Please enter row number and column number seperated by a space.");
        i = keyboard.nextInt();
        j = keyboard.nextInt();
            if(board[i][j] != ' ')
                System.out.println("That position is taken");
        System.out.println("You have entered row" + " " + i + " "+ "and column" + " " + j);
        System.out.println("Thank you for your selection.");
        System.out.print(board[i][j]='X');
        player = 'O';

    }
    else if(player == 'O')
    {
        System.out.println("O's Turn");
        System.out.println("Where do you want your O placed?");
        System.out.println("Please enter row number and column number seperated by a space.");
        i = keyboard.nextInt();
        j = keyboard.nextInt();
            if(board[i][j] != ' ')
                {
                    System.out.println("That position is taken");
                }
        System.out.println("You have entered row"+" " + i +" "+ "and column" +" " + j);
        System.out.println("Thank you for your selection.");
        System.out.println(board[i][j] = 'O');
        player = 'X';

    }

}


public static boolean winner()
{
    //check the row
    for (int i = 0; i < 3; i++)
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && (board[i][0] == 'X'))
        {
            System.out.println("X IS THE WINNER");
            return true;
        }
        else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&(board[i][0]=='O'))
        {
            System.out.println("O IS THE WINNER");
            return true;
        }

        //check the column
        else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i] =='X'))
        {
            System.out.println("X IS THE WINNER");
            return true;
        }
        else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i]=='O'))
        {
            System.out.println("O IS THE WINNER");
            return true;
        }
    }

        //check diagnols from the left
        if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0]=='O'))
        {
            System.out.println("O IS THE WINNER!");
            return true;
        }
        else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0] == 'X'))
        {
            System.out.println("X IS THE WINNER!");
            return true;
        }
        //checks diagnols from the right
        else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2]=='O'))
        {
            System.out.println("O IS THE WINNER!");
            return true;
        }
        else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2]=='X'))
        {
            System.out.println("X IS THE WINNER!");
            return true;
        }
        else
        {
            return false;
        }
}


public static boolean tie()
{
    //If there is no winner(a tie)
    for(int i=0;i<3;i++)
    {
        for(int j=0; j<3;j++)
        {
            if(board[i][j]==' ')
            {
                System.out.println("It's A TIE!");
                return false;
            }
        }
    }
    return true;
}

}







import java.util.Scanner;

public class TicTacToeDemo
{
public static char[][] board = new char[3][3];

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

    int i, j;
    String answer;

    do{

        TicTacToe.newGame();

        while(!TicTacToe.winner())
        {
            TicTacToe.writeBoard();
            TicTacToe.getMove();
            TicTacToe.tie();
        }

        System.out.println("Would you like a rematch?");
        System.out.println("Enter yes or no");
        answer = keyboard.next();

      } while(answer.equalsIgnoreCase("yes"));
    //do-while loop to keep game going if wanted to
}
}
import java.util.Scanner;
公共类Tictatcoe
{
公共静态字符[][]板=新字符[3][3];
公共静态字符播放器;
公共静态void newGame()
{
System.out.println(“新游戏:X先走”);
玩家='X';
}
公共静态无效写板()
{
//绘制游戏板
字符串行=“\uuuuuuuuuuuuuuuuuuuu”;
System.out.println(“”);
//使列和行分开

对于(int i=0;i当您进行第一步时,player变量未定义,因为它是getMove()和newGame()的局部变量。将其设置为全局变量。 此外,您要检查是否有赢家,但不检查哪一个。您需要检查,它是否是“X”或“O”行,然后返回赢家,例如,作为字符,如果没有赢家,则返回其他字符

所以,应该是这样的:

char player;
public static void newGame()
{
    System.out.println("New Game: X goes first.");
    player = 'X';
}
另外,在方法getMove()中:

publicstaticvoidgetmove()
{
扫描仪键盘=新扫描仪(System.in);

char player;TicTacToe类

newGame()缺少返回语句,添加void

播放器未初始化,请使用以下命令将其设置为全局变量:

private static char player;
public static void newGame()
{
  System.out.println("New Game: X goes first.");
  player = 'X';
}
对winner方法进行一些重构,它太复杂了。可能会将条件分解成不同的方法,以便于可读性。你可以找到很多关于如何进行重构的示例

修复错误处理(我输入了“22 33”,得到了ArrayIndexOutOfBoundsException,还有“a b”,我得到了InputMismatchException),您需要防止这种情况

TicTacToeDemo类

板子从不使用,i和j也不使用

为什么要做/while?它可以通过while循环完成

这几个修复使它运行,但您需要解决所有其他问题


您正在使用什么IDE进行开发?它应该报告此错误并向您解释。

我还缺少一条返回语句,不知道该语句与方法winner()有关,您的循环是无用的,因为您正在返回一个值(因此,退出该方法)在第一个循环中。我添加了一些括号,它似乎消失了,谢谢。我尝试使它成为全局的,但它仍然显示错误!感谢您的反馈。我修复了赢家方法。所以,请确保,您首先调用的是newGame()。而且,最重要的是,显示main方法,它可以帮助查找问题。我添加了另一个类以供参考。您是否更改了TictaToe类?请也添加新的编辑。凯,我现在仍然确定如何处理newGame(),正如您所知
private static char player;
public static void newGame()
{
  System.out.println("New Game: X goes first.");
  player = 'X';
}