定义正则表达式为:
String reg="^\\d+$"
获取要判断的字符串:
String str;//可以通过Scanner从控制台输入,也可以用字符串常量进行初始化
调用字符串的matches方法判断字符串为纯数字情况:
str.matches(reg);
如果是纯数字返回为true,否则返回为false;
import java.util.Scannerimport java.util.regex.Pattern
public class Test {
public static void main(String[] args) {
Scanner can = new Scanner(System.in)
String ss = can.nextLine()
String regEx = "[\u4e00-\u9fa5]"
Pattern p = Pattern.compile(regEx)
int num = 0//汉字长度
for(int i=0i<ss.length()i++){
if(p.matches(regEx, ss.substring(i, i + 1))){
num++
}
}
if(num<2){
System.out.println("输入的中文不足两个")
}else{
System.out.println("满足")
}
}
}
....这回题终于看明白.........