如题所述
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String intpatten="[0-9]";
String charpatten="[a-zA-Z]";
String signalpatten="\\W";
int intflag=0;
int charflag=0;
int signalflag=0;
String str=sc.next();
String[] strs = new String[str.length()];
for(int i=0;i<str.length();i++) {
strs[i]=str.substring(i,i+1);
if(strs[i].matches(intpatten))
intflag++;
else if(strs[i].matches(charpatten))
charflag++;
else
signalflag++;
}
System.out.println("字符个数:"+charflag+" 数字个数"+intflag+" 非字符数:"+signalflag);
}
正则表达式做 看不懂追问
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String intpatten="[0-9]";
String charpatten="[a-zA-Z]";
String signalpatten="\\W";
int intflag=0;
int charflag=0;
int signalflag=0;
String str=sc.next();
String[] strs = new String[str.length()];
for(int i=0;i<str.length();i++) {
strs[i]=str.substring(i,i+1);
if(strs[i].matches(intpatten))
intflag++;
else if(strs[i].matches(charpatten))
charflag++;
else
signalflag++;
}
System.out.println("字符个数:"+charflag+" 数字个数"+intflag+" 非字符数:"+signalflag);
}
正则表达式做 看不懂追问
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-11-28
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String intpatten="[0-9]";
String charpatten="[a-zA-Z]";
String signalpatten="\\W";
int intflag=0;
int charflag=0;
int signalflag=0;
String str=sc.next();
String[] strs = new String[str.length()];
for(int i=0;i<str.length();i++) {
strs[i]=str.substring(i,i+1);
if(strs[i].matches(intpatten))
intflag++;
else if(strs[i].matches(charpatten))
charflag++;
else
signalflag++;
}
System.out.println("字符个数:"+charflag+" 数字个数"+intflag+" 非字符数:"+signalflag);
}本回答被提问者采纳
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String intpatten="[0-9]";
String charpatten="[a-zA-Z]";
String signalpatten="\\W";
int intflag=0;
int charflag=0;
int signalflag=0;
String str=sc.next();
String[] strs = new String[str.length()];
for(int i=0;i<str.length();i++) {
strs[i]=str.substring(i,i+1);
if(strs[i].matches(intpatten))
intflag++;
else if(strs[i].matches(charpatten))
charflag++;
else
signalflag++;
}
System.out.println("字符个数:"+charflag+" 数字个数"+intflag+" 非字符数:"+signalflag);
}本回答被提问者采纳
第2个回答 2012-11-28
输入的String .length 就是个数。字母和数字的区分在计算机中是有数字代表的。判读在不在这个范围内就行了。然后再取个数