hasNextInt()方法

hasNextInt()方法是判断控制台接收是否为数字,当你在控制台输入一个字符的时候,hasNextInt()判断你输入这个字符是不是数字,而不是接收值,当if判断通过之后执行接收,也就是你输入的那个字符,你也可以试试 public static void main(String[] args) { Scanner input = new Scanner(System.in); if(input.hasNextInt()){ int num = input.nextInt(); System.out.println(num); }else{ System.out.println("请输入正确数字!~"); } } 当你输入非数字的字符的时候他会直接进入else块,所以说hasNextInt()只起到判断作用而不是接收数据

原文地址:https://www.cnblogs.com/jmsjh/p/7231227.html