方法一:递归异常实现
public static void main(String[] args) {
mainUI();
// centerUI();
}
private static void mainUI() {
int num;
Scanner s;
System.out.println("br");
System.out.println("请输入一个整数:");
while(true){
try{
s = new Scanner(System.in);
num = s.nextInt();
break;
}catch(Exception e){
System.out.println("你输入的必须是整数");
}
}
System.out.println("恭喜你,输入成功");
}
方法二:循环异常判断
public static void main(String[] args) {
mainUI();
// centerUI();
}
private static void mainUI() {
int num;
Scanner s;
System.out.println("br");
System.out.println("请输入一个整数:");
while(true){
try{
s = new Scanner(System.in);
num = s.nextInt();
break;
}catch(Exception e){
System.out.println("你输入的必须是整数");
}
}
System.out.println("恭喜你,输入成功");
}