InputMismatchException:输入的和你想要的不匹配
Scanner有一个hasNextInt()方法,会判断到底符不符合
代码演示
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in);
// 获取数据
if (sc.hasNextInt()) {//可以判断出输入的数据是否合法
int x = sc.nextInt();
System.out.println("x:" + x);
} else {
System.out.println("你输入的数据有误");
}
}