50道编程题之07:输入一行字符,分别统计出其中的英文字母,空格,数字和其他字符的个数

package com.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 莫文龙 on 2018/3/27.
 */

//输入一行字符,分别统计出其中的英文字母,空格,数字和其他字符的个数

public class Demo7 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        char[] chars = str.toCharArray();
        int letterCount = 0;
        int spaceCount = 0;
        int numberCount = 0;
        int otherCount = 0;
        for (char c : chars) {
            boolean falg = true;
            if (c == ' ') {
                spaceCount ++;
                falg = false;
                continue;
            }
            for (int i = 0 ; i <= 9 ; i ++) {
                if (String.valueOf(c).equals(String.valueOf(i))) {
                    numberCount ++;
                    falg = false;
                    break;
                }
            }
            for (int i = 'a' ; i <= 'z' ; i ++) {
                if (i == c) {
                    letterCount ++;
                    falg = false;
                    break;
                }
            }
            for (int i = 'A' ; i <= 'Z' ; i ++) {
                if (i == c) {
                    letterCount ++;
                    falg = false;
                    break;
                }
            }
            if (falg) {
                otherCount ++;
            }
        }
        System.out.println("英文字母的个数" + letterCount);
        System.out.println("空格的个数" + spaceCount);
        System.out.println("数字的个数" + numberCount);
        System.out.println("其他字符的个数" + otherCount);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_38104426/article/details/79712840