您接近了:这对我来说很好:
Scanner input = new Scanner(System.in); //construct scannerwhile(!input.hasNextInt()) { input.next(); // next input is not an int, so consume it and move on}int finalInput = input.nextInt();input.close(); //closing scannerSystem.out.println("finalInput: " + finalInput);通过
input.next()在while循环中调用,您将消耗非整数内容,然后再试一次,直到下一个输入为int为止。



