Java字符串03-統計大小寫字母出現的次數


 

單個字符相比較,實際上比較的是ASCII值

chs[i] >= 'A' && chs[i] <= 'Z'   大寫字母范圍
chs[i] >= 'a' && chs[i] <= 'z'   小寫字母范圍
package doudou;

public class test_Count_String {
    public static void main(String[] args) {
        /**
         * 要求:統計大小寫出現的次數 思路:1.大寫字母的ASCII范圍 2.小寫字母的ASCII的范圍 3.定義2個參數去各自統計 4.輸出
         */
        String a = "AbcsDFjdjjHGoeooeo123@#¥%";
        char[] chs = a.toCharArray();
        System.out.println(chs);
        int small = 0;
        int large = 0;
        for (int i = 0; i < chs.length ; i++) {
            if (chs[i] >= 'A' && chs[i] <= 'Z') {
                large++;

            } else if (chs[i] >= 'a' && chs[i] <= 'z') {
                small++;
            }
            else {
                System.out.println("你肯定輸入的不是 大寫或者小寫!!!:"+chs[i]);
            }

        }
        System.out.println("大寫字母有" + large + "個;小寫字母有" + small + "個");
    }

}

 

 

ASCII碼對照表:http://ascii.911cha.com/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM