0

我的问题与一般游戏算法无关。当我对“你想继续吗?输入真或假。”这个问题回答“真”时,程序输出:

"Player one, throw your shape(rock, paper, scissors)! 
 Player two, throw your shape(rock, paper, scissors)!"  

所以我可以在第一个周期输入值,但不能在第二个周期输入。我该如何解决?谢谢。

代码:

public static void main(String[] args) {

        String playerOne;
        String playerTwo;
        Scanner myScan = new Scanner(System.in);
        boolean playAgain = true;

        do 
        {
            System.out.println("Player one, throw your shape(rock, paper, scissors)! ");
            playerOne = myScan.nextLine();        
            System.out.println("Player two, throw your shape(rock, paper, scissors)! ");
            playerTwo = myScan.nextLine();




            if (playerOne.equals("scissors") && playerTwo.equals("rock"))
            {
            System.out.println("Player one lost! Playr two won!");
            } 

              else if (playerOne.equals("scissors") && playerTwo.equals("paper")) 
              {
              System.out.println("Player one won! Player two lost!");
              } 


              else if (playerOne.equals("rock") && playerTwo.equals("paper")) 
              {
               System.out.println("Player one lost! Player two won!");
              }


              else if (playerOne.equals("rock") && playerTwo.equals("scissors")) 
              {
              System.out.println("Player one won! Player two lost!");
              }


              else if (playerOne.equals("paper") && playerTwo.equals("scissors")) 
              {
              System.out.println("Player one lost! Player two won!");
              }


              else if (playerOne.equals("paper") && playerTwo.equals("rock")) 
              {
              System.out.println("Player one won! Player two lost!");
              }

              System.out.println("Do you want to continue? Enter true or false.");
              playAgain = myScan.nextBoolean();

        }while(playAgain);


    }
} 
4

1 回答 1

4

这是因为Scanner仅读取您请求的布尔值nextBoolean,没有其他内容,甚至没有读取以下行结尾。结束的行然后保留在输入流中,并在循环出现并且您要求时作为空行返回nextLine

相反,您要做的是读取完整的行而不是布尔值playAgain,然后手动解释(建议使用类似的东西myScan.nextLine().equals("yes"))。

于 2013-09-28T04:11:48.483 回答