更新时间:2023-02-17 08:51:14
放 keyboard.nextLine()
在这一行之后:
int answer=keyboard.nextInt();
当您使用 nextLine()时,这是一个常见问题/ code>
nextInt()
扫描程序
类的方法之后的方法。
This is a common problem that usually happens when you use nextLine()
method after nextInt()
method of Scanner
class.
实际发生的情况是,当用户在 int answer = keyboard.nextInt();
中输入一个整数时,扫描仪将仅采用数字和保留换行符 \ n
。所以你需要通过调用 keyboard.nextLine();
来放弃那个新行字符,然后你可以调用 String input = keyboard.nextLine();
没有任何问题。
What actually happens is that when the user enters an integer at int answer = keyboard.nextInt();
, the scanner will take the digits only and leave the new-line character \n
. So you need to do a trick by calling keyboard.nextLine();
just to discard that new-line character and then you can call String input = keyboard.nextLine();
without any problem.