public class Test3 {
/**
* 统计不同字符的个数
* @param args
* 分析:字符串是由字符组成的,而
*/
public static void main(String[] args) {
demo1();
}
private static void demo1() {
String str = "lfasdjfoODHGIOp!$!@142523@#%ajfhiad";
//定义计数器
int big = 0;
int small = 0;
int num = 0;
int other = 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 {
other++;
}
}
System.out.println("大写字母有:" + big + "个,小写字母有"
+small + "个,数字字符有 " + num + "个,其他字符有:" + other + "个");
}
}
|
|