我有这样的事情:
Scanner in=new Scanner(System.in);
int rounds = 0;
while (rounds < 1 || rounds > 3) {
System.out.print("How many rounds? ");
if (in.hasNextInt()) {
rounds = in.nextInt();
} else {
System.out.println("Invalid input. Please try again.");
System.out.println();
}
// Clear buffer
}
System.out.print(rounds+" rounds.");
如何清除缓冲区?
编辑:我尝试了以下方法,但由于某种原因它不起作用:
while(in.hasNext())
in.next();
最佳答案
试试这个:
in.nextLine();
这会使扫描器前进到下一行。
关于java - 如何清除 Java 中的扫描仪缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604125/