题解 | #统计字符#用系数统自带的函数判断字符属于哪一类

统计字符

http://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()) { // 注意 while 处理多个 case
            int letterNum = 0,whitespaceNum = 0,digitNum = 0,otherNum = 0;
            String str = in.nextLine();
            for(Character ch : str.toCharArray()){
                if(Character.isLetter(ch)){
                    letterNum++;
                }else if(Character.isDigit(ch)){
                    digitNum++;
                }else if(Character.isWhitespace(ch)){
                    whitespaceNum++;
                }else{
                    otherNum++;
                }
            }
            System.out.println(letterNum);
            System.out.println(whitespaceNum);
            System.out.println(digitNum);
            System.out.println(otherNum);
        }
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
09-15 12:11
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务