--- tags: Cmoney_Java題目 --- Java_Cmoney_ft9103_統計字元類型 === ![](https://i.imgur.com/LTO6Lay.png) ![](https://i.imgur.com/ymxfw0i.png) 1.主程式 --- 1. 測試裡面可能有空白,所以去除`int n = sc.nextInt();`,用`sc.nextLine();`直接清空 Scanner (不然會 Runtime Error) 2. 將讀入字串裡的英文字母轉成大寫字母,這樣就不用處理大小寫問題 3. 使用`.charAt()`讀取字串的字元,再將其轉成ASII(轉成 int)判斷 ```java= import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); sc.nextLine(); String str = sc.nextLine(); str = str.toUpperCase(); int[] numbers = new int[4]; for (int i = 0; i < str.length(); i++) { if ((int) str.charAt(i) >= 65 && (int) str.charAt(i) <= 90) numbers[0]++; else if ((int) str.charAt(i) == 32) numbers[1]++; else if ((int) str.charAt(i) >= 48 && (int) str.charAt(i) <= 57) numbers[2]++; else numbers[3]++; } for (int i : numbers) { System.out.println(i); } } } ```