题解 | #统计字符#

统计字符

http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

干净清爽无套

import java.util.Scanner;

/**
 * @author 
 * Created by Administrator on 2022-05-06 03:55
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();
        doCountChar(a);

    }

    private static void doCountChar(String string) {
        char[] arr = string.toUpperCase().toCharArray();
        int aCount = 0,nullCount= 0,numberCount= 0,otherCount = 0;
        for (char a:arr) {
            if(a >= 'A' && a <= 'Z') {
                aCount++;
            }else if(a == ' '){
                nullCount++;
            }else if(a >= '0' && a <= '9' ){
                numberCount++;
            }else{
                otherCount++;
            }

        }

        System.out.println(aCount);
        System.out.println(nullCount);
        System.out.println(numberCount);
        System.out.println(otherCount);
    }

}

全部评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务