您可能像
nextInt()以前那样调用方法。这样的程序是这样的:
Scanner scanner = new Scanner(System.in);int pos = scanner.nextInt();System.out.print("X: ");String x = scanner.nextLine();System.out.print("Y: ");String y = scanner.nextLine();演示您所看到的行为。
问题是
nextInt()不消耗
'n',因此下一个调用将
nextLine()消耗它,然后等待读取的输入
y。
您需要
'n'在调用之前消耗
nextLine()。
System.out.print("X: ");scanner.nextLine(); //throw away the n not consumed by nextInt()x = scanner.nextLine();System.out.print("Y: ");y = scanner.nextLine();(实际上,更好的方法是在
nextLine()之后直接调用
nextInt())。



