zoukankan      html  css  js  c++  java
  • 输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

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

     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);
            }
    
        }
  • 相关阅读:
    vb笔记
    linux学习笔记
    linnux--shell
    # 用类来封装动态数组:分文件编写
    面向对象
    c++2
    c++1
    答疑:指针数组字符串
    文件操作
    用函数封装实现对一个数组增删改查
  • 原文地址:https://www.cnblogs.com/dongma/p/13234187.html
Copyright © 2011-2022 走看看