java编程中如何用程序判断输入的数是整型还是实型

2025-05-09 10:12:29
推荐回答(2个)
回答1:

String input = "";
try{
Integer.parseInt(input);
System.out.println("整数");
}catch(NumberFormatException nonIntegerExp){
try{
Double.parseDouble(input);
System.out.println("是double类型");
}catch(NumberFormatException nonDoubleExp){
System.out.println("不是int,也不是double");
}
}

回答2:

用instanceof
如if(Integer instanceof value){

}