toCharArray()将字符串分割成一个个字符

package homework;
//2、其中数字有几个?大写字母有几个?小写字母有几个?
public class practice_07 {
    //数字48-57,大写字母65-90,小写字母97-122
    public static void main(String[] args) {
        String str ="at7cH63OPbQ81";
        int count = 0 , count1 = 0 , count2 = 0;
        //将字符串所有字符分割并存放进字符数组ch
        char[] ch = str.toCharArray();
        for (char c : ch){  //遍历字符数组
            if (c >= 48 && c<= 57){ //如果字符的Ascii码在48-57之间则为数字,计数加1
                count ++;
            }
            else if (c >= 65 && c <= 90){   //如果字符的Ascii码在65-90之间则为大写字母,计数加1
                count1 ++;
            }
            else if (c >= 97 && c <= 122){  //如果字符的Ascii码在97-122之间则为小写字母,计数加1
                count2 ++;
            }
        }
        System.out.println("数字一共有" + count + "个");
        System.out.println("大写字母一共有" + count1 + "个");
        System.out.println("小写字母一共有" + count2 + "个");
    }
}

猜你喜欢

转载自blog.csdn.net/yl23921/article/details/126736464
今日推荐