【Java例题】5.3 字符统计

3.分别统计一个字符串中大写字母、小写字母、数字、 汉字以及其它字符的个数。 

package chapter5;
import java.util.Scanner;

public class demo3 {
    public static void main(String[] args) {
        int da=0;
        int xiao=0;
        int shu=0;
        int han=0;
        int els=0;
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        for(int i=0;i<str.length();i++) {
            char c=str.charAt(i);
            if(c>='A'&&c<='Z') {
                da=da+1;
            }else if(c>='a'&&c<='z'){
                xiao=xiao+1;
            }else if(c>='0'&&c<='9') {
                shu=shu+1;
            }else if(c>=0x4E00&&c<=0x9FA5) {
                han=han+1;
            }else {
                els=els+1;
            }
        }
        System.out.println("大写数量:"+da);
        System.out.println("小写数量:"+xiao);
        System.out.println("数字数量:"+shu);
        System.out.println("汉字数量:"+han);
        System.out.println("其他数量:"+els);
        sc.close();
    }
}

猜你喜欢

转载自www.cnblogs.com/LPworld/p/10724038.html