退出或继续java游戏

退出或继续java游戏,java,Java,在程序输出赢家后,我询问用户是否想再次播放或退出,但由于某些原因扫描仪无法读取此输入(是否继续)。我甚至在while循环之外提示继续,但仍然不走运: “|XXX| |O| |O| 玩家X是赢家! 是否要再次播放?输入Y表示是,或输入N表示否! 构建成功(总时间:26秒) " 这是代码: public class TicTacToeRunner { public static void main(String[] args) { int row; int col

在程序输出赢家后,我询问用户是否想再次播放或退出,但由于某些原因扫描仪无法读取此输入(是否继续)。我甚至在while循环之外提示继续,但仍然不走运: “|XXX| |O| |O|

玩家X是赢家! 是否要再次播放?输入Y表示是,或输入N表示否! 构建成功(总时间:26秒) "

这是代码:

public class TicTacToeRunner
{
   public static void main(String[] args)
   {
      int row;
      int column;
      Scanner in = new Scanner(System.in);
      TicTacToe game = new TicTacToe();
      String player = "X";
      boolean done = false; 
      int moveCounter = 0;
      String answer;


       int noMovement = -2;
       int toQuit = -1;
       int movesToCheckWins = 5;
       int movesToCheckTies = 7;



      while (!done) 
      {

         do
         {
            row = column = noMovement;

            System.out.print("\n" + game);
            System.out.print("Please make a move " + (moveCounter + 1) + "(" + player + ")\nRow for " + player.toUpperCase() + " (or -1 to exit): ");

            if (in.hasNextInt()) //check if input is an integer
             {
               row = in.nextInt();
             }

             if (row == toQuit) //if entered -1 quit the game
             {
                 done = true;
                 System.out.println("Player " +player.toUpperCase() + " ended the game !");
                 System.exit(0); //game termination
             }
             else
             {
                System.out.print("Column for " + player.toUpperCase() + ": ");
                if(in.hasNextInt()) //check if input is an integer
                 {
                  column = in.nextInt();
                 }
             }
         }while(!game.checkForValidMove(row, column)); //end of do-while loop if checkForValidMove is false

        moveCounter++;
        game.set(row, column, player);

        if (moveCounter >= movesToCheckWins) //check wins after 5 moves
        {
            if (game.checkForWin(row, column, player)) //if a winner
            {
                done = true;
                System.out.println("\n" + game);
                System.out.println("Player " + player.toUpperCase() + " is a winner!");

            }
        }

        if (moveCounter >= movesToCheckTies) //Check for ties after 7 moves
        {
            if (game.checkForEarlyTie()) //check for early tie
            {
                done = true;
                System.out.println("\n" + game);
                System.out.println("Tie Game after " + moveCounter + " moves!");

            }
        }


     // Switching players 

     if (player.equals("X"))
     {
           player = "O";
     }
        else
     {
           player = "X";
     }


      }


                System.out.println("Do you want to play again? Enter Y for yes or N for no!");

                answer = in.nextLine();
                answer = answer.toLowerCase(); //change input to lowercase for bullet proofing
                if(answer.equals("y"))
                {
                done = false;
                player = "X";
                moveCounter = 0;
                game.resetTheGame();

                }

              else
                 {
                 System.exit(0); //program termination
               }


  }

}

使用执行while循环。你的工作很容易

import java.util.Scanner;

public class TicTacToeRunner {
    public static void main(String[] args) {
        int row;
        int column;
        Scanner in = new Scanner(System.in);
        TicTacToe game = new TicTacToe();
        String player = "X";
        boolean done = false;
        String choice = "";
        int moveCounter = 0;
        String answer;

        int noMovement = -2;
        int toQuit = -1;
        int movesToCheckWins = 5;
        int movesToCheckTies = 7;
        do{
        while (!done) {
            do {
                row = column = noMovement;

                System.out.print("\n" + game);
                System.out.print("Please make a move " + (moveCounter + 1) + "(" + player + ")\nRow for " + player.toUpperCase() + " (or -1 to exit): ");

                if (in.hasNextInt()) // check if input is an integer
                {
                    row = in.nextInt();
                }

                if (row == toQuit) // if entered -1 quit the game
                {
                    done = true;
                    System.out.println("Player " + player.toUpperCase() + " ended the game !");
                    System.exit(0); // game termination
                } else {
                    System.out.print("Column for " + player.toUpperCase() + ": ");
                    if (in.hasNextInt()) // check if input is an integer
                    {
                        column = in.nextInt();
                    }
                }
            } while (!game.checkForValidMove(row, column)); // end of do-while
                                                            // loop if
                                                            // checkForValidMove
                                                            // is false

            moveCounter++;
            game.set(row, column, player);

            if (moveCounter >= movesToCheckWins) // check wins after 5 moves
            {
                if (game.checkForWin(row, column, player)) // if a winner
                {
                    done = true;
                    System.out.println("\n" + game);
                    System.out.println("Player " + player.toUpperCase() + " is a winner!");

                }
            }

            if (moveCounter >= movesToCheckTies) // Check for ties after 7 moves
            {
                if (game.checkForEarlyTie()) // check for early tie
                {
                    done = true;
                    System.out.println("\n" + game);
                    System.out.println("Tie Game after " + moveCounter + " moves!");

                }
            }

            // Switching players

            if (player.equals("X")) {
                player = "O";
            } else {
                player = "X";
            }

        }

        System.out.println("Do you want to play again? Enter Y for yes or N for no!");
        choice = in.nextLine();
        }while(choice.equals("y")||choice.equals("Y"));

    }

}

while(choice.equalsIgnoreCase(“y”))怎么样;仍然不工作“玩家O是赢家!你想再次玩吗?输入Y表示是或N表示否!生成成功(总时间:1分1秒)”输出扫描程序不想读取任何输入后我不能键入“Y”或“N”你是否使用System.exit的任何其他功能