1 package JavaDemo;
2 import java.util.Scanner;
3 /*
4 题目:
5 键盘输入一个字符串,并且统计其中各种字符出现的次数。
6 种类有:大写字母,小写字母,数字,其他
7 */
8 public class WorkDemo02 {
9 public static void main(String[] args) {
10 Scanner sc = new Scanner(System.in);
11 System.out.println("请输入一个字符串:");
12 String input = sc.next();//获取一个字符串
13
14 //计数器
15 int countUpper = 0; //大写字母
16 int countLower = 0; //小写字母
17 int countNumber = 0;//数字
18 int countOther = 0; //其他
19 //将字符串转换为字符数组
20 char[] charArray = input.toCharArray();
21 for (int i = 0; i < charArray.length; i++) {
22 char ch = charArray[i];//当前单个字符
23 if ('A' <= ch && ch <= 'Z') {
24 countUpper++; //大写字母
25 } else if ('a' <= ch && ch <= 'z') {
26 countLower++;
27 } else if ('0' <= ch && ch <= '9') {
28 countNumber++;
29 } else {
30 countOther++;
31 }
32 }
33 System.out.println("大写字母有:"+countUpper);
34 System.out.println("小写字母有:"+countLower);
35 System.out.println("数字写字母有:"+countNumber);
36 System.out.println("其他字母有:"+countOther);
37 }
38 }