int i=(int)f
if(i==f)
System.out.println("是整数")
else
System.out.println("不是整数")
有多种方法,以下提供两例:
public static void main(String[] args){int i = 1111
double f = 111.111
//方法1
boolean b = (int)i-i==0
if(b){
System.out.println("整数")
}else{
System.out.println("非整数")
}
boolean c = (int)f-f==0
if(c){
System.out.println("整数")
}else{
System.out.println("非整数")
}
//方法2
if(i==(long)i){
System.out.println("整数")
}else{
System.out.println("非整数")
}
if(f==(long)f){
System.out.println("整数")
}else{
System.out.println("非整数")
}
}
可以通过来自String类中的matches("")方法进行判断,此方法里存放的是一个regex正则表达式。返回的是一个boolean类型的变量。true:验证通过false:验证失败。String str="123"
boolean result=str.matches("[0-9]+$")
System.out.println(result)