如何在 Java 中清除扫描程序缓冲区?
我有这样的东西:
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();