题解 | #统计字符#

统计字符

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

import java.util.Scanner;

// 想说什么却不知说什么
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) {
            String line = in.nextLine();
            int eng = 0;
            int nul = 0;
            int num = 0;
            int other = 0;

            for (int i = 0; i < line.length(); i++) {
                char curCh = line.charAt(i);
                if ((curCh >= 'a' && curCh <= 'z' ) || (curCh >= 'A' && curCh <= 'Z')) {
                    eng++;
                } else if (curCh == ' ') {
                    nul++;
                } else if (curCh >= '0' && curCh <= '9') {
                    num++;
                } else {
                    other++;
                }
            }

            System.out.println(eng);
            System.out.println(nul);
            System.out.println(num);
            System.out.println(other);
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务