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);
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章