private static void demo1() {
//设置字符串
String str = "slsdadfjHWlksddfHslHSlSDH!%^&*(33225DSFs2";
//定义计数器
int count = 0;
//遍历字符串
for (int i = 0; i < str.length(); i++) {
//获取每个字符
char ch = str.charAt(i);
//判断字符是否为大写,满足计数器就自增
if (ch >= 'A' && ch <= 'Z') {
count++;
}
}
//输出大写字符的个数
System.out.println(count);
}
|