package cn.itcast_04;
/*
* 需求:统计一个字符串大写字母字符,小写字符,数字字符出现的次数
* 举例:
* “Hello123Word”
* 结果:
* 大写字符:2个
* 小写字符:8个
* 数字字符:3个
* 分析:
* A:定义三个统计变量
* bigCount=0
* smallCount=0
* numberCount=0
* B:遍历字符串,得到每个字符
* C:判断该字符到底是属于那种类型的
* 大:bigCount++
* 小:smallCount++
* 数字: numberCount++
* 这个题目的难点就是如何判断某个字符是大的,小的,还是数字
* ASCII码表
* 0 48
* A 65
* a 97
*
* 虽然,我们按照数字的这种比较是可以的,但是想多了,有个更简单的
* cahr ch = s.charAt(x);
* (ch>='0' && ch <='9')在比较的时候char类型的会先转成int类型的
* ch会转‘0’也会转
* if(ch>='0' && ch <='9')
* if(ch>='a' && ch<='z')
* if(ch>='A' && ch<='z')
*
*
*/
public class StringDome_统计 {
public static void main(String[] args) {
//定义字符串
String s = "Hello123Word";
//定义三个变量
int bigCount = 0;
int smallCount=0;
int numberCount=0 ;
//遍历字符串
for(int x=0;x<s.length();x++) {
char ch = s.charAt(x);
//判断该字符到底是属于那种类型的
if(ch>='a' && ch<='z') {
smallCount++;
}else if(ch>='A' && ch<='z') {
bigCount++;
}else if(ch>='0' && ch <='9') {
numberCount++ ;
}
}
//输出结果
System.out.println("大写字母"+bigCount);
System.out.println("小写字母"+smallCount);
System.out.println("数字"+numberCount);
}
}