题解 | #统计字符#--利用if分支

统计字符

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            char[] chars = str.toCharArray();
            int[] result = new int[4];
            // 利用if分支
            for (char chr : chars) {
                if ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z')) {
                    result[0]++;
                } else if (chr == ' ') {
                    result[1]++;
                } else if (chr >= '0' && chr <= '9') {
                    result[2]++;
                } else {
                    result[3]++;
                }
            }
            Arrays.stream(result).forEach(System.out :: println);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务