黑马程序员技术交流社区

标题: 统计不同字符的个数 [打印本页]

作者: 荒丶    时间: 2016-4-10 14:00
标题: 统计不同字符的个数
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 + "个");
        }

}

作者: 65708535    时间: 2016-4-10 14:52
如果是中文字符的话该怎么办啊,提示错误




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2