public class Test3 {
public static void main(String[] args) {
String str="ABCDEabcd123456!@#2$^*";
int big=0;
int small=0;
int num=0;
int outher=0;
for(int i=0;i<str.length();i++) {//循环遍历
char c=str.charAt(i);//通过索引获取到每一个字符
if(c>='A'&&c<='Z') {
big++;
}
else if(c>='a'&&c<='z') {
small++;
}
else if(c>='0'&&c<='9') {
num++;
}
else {
outher++;
}
}
System.out.println("字符串中:大写字母有"+big+"个,小写字母有:"+small+"个,数字字符:"+num+"个,其他字符:"+outher+"个");
}
}
运行结果:
字符串中:大写字母有5个,小写字母有:4个,数字字符:7个,其他字符:6个