首页 > 其他 > 详细

输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

时间:2020-07-04 11:15:49      阅读:43      评论:0      收藏:0      [点我收藏+]

思路:这题还是比较简单的,直接遍历判断即可

 public static void main(String[] args) {
       int strCount = 0;
       int blankCount = 0;
       int numCount = 0;
       int otherCount = 0;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.nextLine();
            char[] chars = str.toCharArray();
            for (char c : chars) {
                if((c >= ‘a‘ && c <= ‘z‘) || (c >= ‘A‘ && c <= ‘Z‘)){
                    strCount++;
                }
                else if(c == ‘ ‘){
                    blankCount++;
                }
                else if((c >= ‘0‘ && c <= ‘9‘)){
                    numCount++;
                }
                else {
                    otherCount++;
                }
            }
            System.out.println(strCount);
            System.out.println(blankCount);
            System.out.println(numCount);
            System.out.println(otherCount);
        }

    }

 

输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

原文:https://www.cnblogs.com/dongma/p/13234187.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!