Scanner in Java not working

后端 未结 2 492
一个人的身影
一个人的身影 2021-01-02 20:59

I\'m trying to write a very simple number guessing game (code is below). After 1 round is finished, the user is supposed to be able to decide whether he/she wants to play an

2条回答
  •  遥遥无期
    2021-01-02 21:16

    Use scan.next()+ scan.nextLine(); instead eg.

    Scanner scan = new Scanner(System.in);
    
    String s = scan.nextLine() +scan.nextLine();
    

    Problem occurs because the last newline character for the last line of input is still queued in the input buffer and the next nextLine() will be reading the remainder of the line (which is empty). So, when you use next it goes to the next token, then you can get the remaining input using nextLine()

提交回复
热议问题