2013-11-14 8 views
0

afer program выводит победителя, я спрашиваю, хочет ли пользователь снова играть или выйти, но по какой-то причине сканер не читает этот вход (продолжить или нет). Я даже поставить запрос на продолжение вне цикла в то время, но до сих пор не повезло: «| XXX | | O | | O |выйти или продолжить игру в java

Player X является победителем вы хотите играть снова Do Введите Y для да!? или Н в течение не строить успешное (общее время: 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 
       } 


    } 

} 

ответ

0

с использованием делать во время петли. ваша работа проста ..

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")); 

    } 

} 
+1

как насчет while (выбор.equalsIgnoreCase ("y")); –

+0

hm намного лучше. – subash

+0

до сих пор не работает « Игрок O победитель Вы хотите снова играть Введите Y для да или N для не BUILD УСПЕШНОГО (общее время: 1 минута 1 секунда)!?!» после выхода сканера не хочет читать какие-либо входы – Timur

Смежные вопросы