public static void main(String[] args) { // 在控制檯輸入字符串,統計出大寫的字符的個數,小寫的字符個數,數字類型的字符個數,以及其餘字符的個數 Scanner sc = new Scanner(System.in); //控制檯輸入字符串 int big = 0; //初始化字符個數 int small = 0; int num = 0; int other = 0; System.out.println("請輸入一串字符串"); String str = sc.next(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= 65 && str.charAt(i) <= 90) { big++; } else if (str.charAt(i) >= 97 && str.charAt(i) <= 122) { small++; } else if (str.charAt(i) >= 48 && str.charAt(i) <= 57) { num++; } else { other++; } } System.out.println("大寫字母有:" + big + "個"); System.out.println("小寫字母有:" + small + "個"); System.out.println("數字有:" + num + "個"); System.out.println("其餘字符有:" + other + "個"); }