/**
* 键盘输入一个字符串,并且统计其中各种字符出现的次数
* 种类有:大写字母,小写字母,数字,其它
*/
import java.util.Scanner;
/**
* 键盘输入一个字符串,并且统计其中各种字符出现的次数
* 种类有:大写字母,小写字母,数字,其它
*/
public class CountDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.next();
char[] array = str.toCharArray();
//统计小写字母个数
int count1 = 0;
//统计大写字母个数
int count2 = 0;
//统计数字个数
int count3 = 0;
//统计其它个数
int count4 = 0;
System.out.println(array.length);
for (int i = 0; i < array.length; i++) {
//两种写法
if (array[i] >= 97 && array[i] <= 122) {//if (array[i] >= 'A' && array[i] <= 'Z')
count1++;
} else if (array[i] >= 65 && array[i] <= 90) { //else if(array[i] >='A' && array[i] <= 'Z')
count2++;
} else if (array[i] >= 48 && array[i] <= 57) {//}else if(array[i]>='0' && array[i]<='9'){
count3++;
} else {
count4++;
}
}
System.out.println("小写字母个数为:" + count1);
System.out.println("大写字母个数为:" + count2);
System.out.println("数字字符个数为:" + count3);
System.out.println("其它字符个数为:" + count4);
}
}
![](https://img-blog.csdnimg.cn/20200107225014466.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xVX0xlbw==,size_16,color_FFFFFF,t_70)