String类练习统计一个字符串中大小写字母及数字字符个数


public class StringPractice {
    public static void main(String[] args) {
        //创建一个文本扫描器
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.next();

        //定义三个变量用来统计大写字母,小写字母,数字的个数
        int bigCount = 0;
        int smallCount = 0;
        int numCount = 0;

        //遍历字符串,对每个字符进行判断
        for (int i = 0;i<str.length();i++){
            char chs = str.charAt(i);
            //如果
            if (chs >= 'A' && chs <= 'Z'){
                bigCount++;
            }else if (chs >= 'a' && chs <= 'z'){
                smallCount++;
            }else if (chs >= '0' && chs <= '9'){
                numCount++;
            }else {
                System.out.println("该字符"+chs+"非法");
            }
        }
        //输出结果
        System.out.println("大写字符:"+bigCount+"");
        System.out.println("小写字符:"+smallCount+"");
        System.out.println("数字字符:"+numCount+"");


    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM