Here is the skeleton of some basic code I am writing to make a simple game:
Scanner in = new Scanner(System.in);
String name;
String playing;
int age;
do {
System.out.println("Enter your name");
name = in.nextLine();
System.out.println("Enter your age");
age = in.nextInt();
System.out.println("Play again?");
playing = in.nextLine();
} while (true);
举例来说,《守则》并不像预期的那样发挥作用,这里就是《守则》的预期运作:
Enter your name
John
Enter your age
20
Play again?
Yes
Enter your name
Bill
...
然而,还有一个问题是“游戏”再行读,这是实际产出:
Enter your name
John
Enter your age
20
Play again?
Enter your name
As you can see "Enter your name" is being displayed again before "Play again?" is able to accept input. When debugging the playing variable is set to "", so there is no input that I can see and I cannot figure out what is being consumed. Any help would be appreciated, thanks!