统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数

/*统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)*/

import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();//输入
        int b = 0,s = 0,count= 0;//计数器
        for(int i =0;i<str.length();i++) {
            char ch = str.charAt(i);
            if(ch >= 'A' && ch <= 'Z')
                b++;
            else if(ch >= 'a' && ch <= 'z')
                s++;
            else if(ch >= '0' && ch <= '9')
                count++;
        }
        System.out.println("大写字母的个数是" + b);
        System.out.println("小写字母的个数是" + s);
        System.out.println("数字字符的个数是" + count);
        

    }

}

猜你喜欢

转载自blog.csdn.net/dianmomanxue/article/details/81291202
今日推荐