统计字符串中各种字符的个数!
public class TongJi {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//给一个字符串,可以用Scanner输入
String str = "bc1A122n好%";
int x = 0;// 大写
int y = 0;// 小写
int z = 0;// 数字
int c = 0;// 其他
for (int i = 0; i < str.length(); i++) {
//根据ASCALL判断字符属于什么类型
if (str.charAt(i) > 64 && str.charAt(i) < 91) {
x++;
} else if (str.charAt(i) > 96 && str.charAt(i) < 123) {
y++;
}else if(str.charAt(i)>47 &&str.charAt(i)<58){
z++;
}else {
c++;
}
}
System.out.println("大写字母个数为 "+x);
System.out.println("小写字母个数为 "+y);
System.out.println("数字个数为 "+z);
System.out.println("其他字符个数为 "+c);
}
}