黑马程序员技术交流社区

标题: 分析加代码统计字符串大小写数字特殊符号出现的次数 [打印本页]

作者: 路好营    时间: 2015-11-11 23:38
标题: 分析加代码统计字符串大小写数字特殊符号出现的次数
统计字符串大小写数字特殊符号出现的格式
package com.heima.text;

public class Text3 {

        /**
         *  需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。
                ABCDEabcd123456!@#$%^
                分析:
                遍历后比较对应的字符,如果符合后自增,最后输出次数。
         */
        public static void main(String[] args) {
                String s = "ABCDEabcd123456!@#$%^";
                int big = 0;
                int small = 0;
                int num = 0;
                int other = 0;
                for (int i = 0; i < s.length(); i++) {
                        char c = s.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 + "个其他字符");
        }

}






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