统计字符串

package com.yan.day10string;

import java.util.Scanner;

public class StringTest03 {
    /*
        请编写程序,由键盘录入一个字符串,
        统计字符串中英文字母和数字分别有多少个。
     */
    public static void main(String[] args) {
        //遍历字符串:(键盘录入字符串)
        System.out.println("请输入一个字符串:");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();

        //计数器:
        int bigCount = 0;//大写字母
        int smallCount = 0;//小写字母
        int numberCount = 0;//数字
        /*
         * a~z : 97--122
         * A~Z : 65--90
         * 0~9 : 48--57
         * */
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (c >= 48 && c <= 57) {
                numberCount++;
            }
            if (c >= 65 && c <= 90) {
                bigCount++;
            }
            if (c >= 97 && c <= 122) {
                smallCount++;
            }
        }
        System.out.println("大写字母有:" + bigCount + "个。");
        System.out.println("小写字母有:" + smallCount + "个。");
        System.out.println("数字有:" + numberCount + "个。");
    }
    //统计字符串中字符出现的次数:


}

你可能感兴趣的:(java,开发语言)