A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 荒丶 中级黑马   /  2016-4-10 14:00  /  937 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test3 {

        /**
         * 统计不同字符的个数
         * @param args
         * 分析:字符串是由字符组成的,而
         */
        public static void main(String[] args) {
                demo1();
               
        }

        private static void demo1() {
                String str = "lfasdjfoODHGIOp!$!@142523@#%ajfhiad";
                //定义计数器
                int big = 0;
                int small = 0;
                int num = 0;
                int other = 0;
               
                //循环  拿到每个字符,在进行判断
                for (int i = 0; i < str.length(); i++) {
                        char c = str.charAt(i);
                        //做判断
                        if(c >= 'A' && c <= 'Z') {
                                big++;
                        }else if (c >= 'a' && c <= 'z') {
                                small++;
                        }else if (c >= '0' && c <= '9') {
                                num++;
                        }else {
                                other++;
                        }
                }
               
                System.out.println("大写字母有:" + big + "个,小写字母有"
                                +small + "个,数字字符有 " + num + "个,其他字符有:" + other + "个");
        }

}

1 个回复

倒序浏览
如果是中文字符的话该怎么办啊,提示错误
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马