Code doesn't wait for user input?

前端 未结 4 1580
春和景丽
春和景丽 2021-01-22 06:29

I have to do a project for my Computer Science class. The problem is:

Patrons of a library can borrow up to three books. A patron, therefore, has a name and up to three

相关标签:
4条回答
  • 2021-01-22 06:38

    nextLine eats the newline character. nextInt leaves it in the input buffer, and the next readLine terminates immediately.

    Quick fix: use readLine for everything, then parse the int from string read.

    0 讨论(0)
  • 2021-01-22 06:38

    You just have to go to the next line. input.nextLine();

    0 讨论(0)
  • 2021-01-22 06:40

    You can use nextInt(); to cause the the input to stop and wait for a response.

    0 讨论(0)
  • 2021-01-22 06:42

    You use nextInt() on line 71 of your code, which gets the integer answer the user provides. Then you use nextLine() which Advances this scanner past the current line and returns the input that was skipped.. The input that is skipped is only the newline character from the previous nextInt() call (It doesn't read the whole line only the int).

    You can skip this by calling input.nextLine() once before you want the input, or by using nextLine() instead of nextInt() and converting the string to the integer value.

    0 讨论(0)
提交回复
热议问题