package lesson04;
public class demo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//案例:统计不同类型字符个数
String password = "abc23Asd^4";
//密码:大写,小写,数字,特殊字符
//1.声明变量
int big = 0;//大写
int small = 0;//小写
int num = 0;//数字
int other = 0;//特殊字符
//2.遍历字符串的字符
for(int i=0;i<password.length();i++) {
//取字符
char ch = password.charAt(i);
System.out.println(ch);
//判断字符是否为大写
if(ch >= 'A' && ch <='Z') {
big ++;
}else if(ch >= 'a' && ch<='z') {
small ++;
}else if(ch >= '0' && ch <= '9') {
num ++;
}else {
other ++;
}
}
System.out.println("大写字母出现的次数:"+big);
System.out.println("小写字母出现的次数:"+small);
System.out.println("数字出现的次数:"+num);
System.out.println("特殊字符出现的次数:"+other);
}
}
统计不同类型字符个数
最新推荐文章于 2023-10-10 12:59:45 发布