大概知道的简单的2种方式 public static void test(){ Scanner scn = new Scanner(System.in) int i = scn.nextInt()// 第一种方式,直接用nextInt方法,这样就限制了用户在控制台只能输入数字了 // 第二种,给用户输入字符串的操作 String s = scn.next() while (true) { try { i = Integer.parseInt(s) break// 假如强转成功,则终止循环 } catch (Exception e) { s = scn.next()//强转失败,继续输入 } } }