輸入一行字符 分別統計其中英文字母 空格 數字 和其他字符的個數

/**
 * @Description:輸入一行字符 分別統計其中英文字母 空格 數字 和其他字符的個數
 * @author :FLY
 * @date :2017年8月10日
 */
public class TestDemo1 {
    public static void main(String[] args) {
        int count1 = 0;// 字母個數
        int count2 = 0;// 空格個數
        int count3 = 0;// 數字個數
        int count4 = 0;// 其他個數
        int length = args[0].length();
        /**  
         * 使用character調用方法,判斷字符的類型
         */
        /*char x[] = args[0].toCharArray();
        for (int i = 0; i < x.length; i++) {
            if (Character.isDigit(x[i])) {
                count2++;
            } else if (Character.isLetter(x[i])) {
                count1++;
            } else if (Character.isSpace(x[i])) {
                count3++;
            } else {
                count4++;
            }
        }*/
        /**
         * 對每一個字符做比較。
         */
        // for (int i = 0; i < length; i++) {
        // char c = args[0].charAt(i);
        // if (c >= '0' && c <= '9') {// 數字 結果14
        // count1++;
        // } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {//
        // 字母
        // // 結果20
        // count2++;
        // } else if (c == ' ') {// 空格 結果10
        // count3++;
        // } else {// 其他 結果21
        // count4++;
        // }
        // }
        System.out.println("字符串長度----:" + length);
        System.out.println("數字個數----:" + count1);
        System.out.println("字母個數----:" + count2);
        System.out.println("空格個數----:" + count3);
        System.out.println("其他個數----:" + count4);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章