java中判断输入的是否是整数

java中判断输入的是否是整数,第1张

直接转化因为JAVA中输入数据的时候 都是字符串
要用IntegerparseInt()转化
如果不是整数会报异常的 你直接根据是否有异常来控制是否继续让用户输入
应LZ要求
下面我给你写的例子
import javaio ;
public class number
{
public static void main(String []s)
{
int num ;
String st ;

BufferedReader br=new BufferedReader(new InputStreamReader(Systemin)) ;

while(true)
{
Systemoutprint("请输入数字:") ;

try
{
st=brreadLine() ;
num=IntegerparseInt(st) ;
break ;
}
catch(Exception e)
{
Systemoutprintln("只能输入整数") ;
}
}
Systemoutprintln("你输入的整数是:"+num) ;
}
}

public class Type{
public static void main(String[] args) {

Systemoutprintln(new Type()type(Mathsqrt(y)));
}
public String type(double num){
String numberType = num%1==0 "他是整数" : "他不是整数";
return numberType;
}
}

import javautilScanner;
public class Test3 {
public static void main(String[] args){
Systemoutprintln("输入");
Scanner scan=new Scanner(Systemin);
int a=scannextInt();
while(true){
if(a>0){
Systemoutprintln("是正数");
break;
}
else{
Systemoutprintln("不是正数,再次输入");
a=scannextInt();
}
}

}
}

非零正整数
public static boolean IsIntNumber(String str) {
String regex = "^\\+[1-9][0-9]$";
return match(regex, str);
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13405768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-30
下一篇 2023-07-30

发表评论

登录后才能评论

评论列表(0条)

保存