import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入四位整数");
int num = sc.nextInt();
String str = String.valueOf(num);
if(str.matches("[1-9][0-9]{3}")) {
System.out.println("是四位整数");
} else {
System.out.println("不是四位整数");
}
}
}
这个做法 很多, 我也不知道什么方法最优
转换成 int 看能否正常转换, 如果能正常转换 再转换成字符串看度
不会用正则表达式,用str.length不是能得到字符串的长度么。
可以使用正则表达式