Scanner 設計定位於「標記化」輸入解析(以分隔符分割),而 char 是單一字元,不具備標記特性。next() 回傳 String,取首字元即可獲取 char。
// 方式一:讀取字串取首字元
char c = scanner.next().charAt(0);
// 方式二:使用 System.in.read() (需處理換行符)
int ascii = System.in.read(); // 回傳 ASCII 碼
char c = (char) ascii;
// 方式三:BufferedReader 讀取整行
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c = br.readLine().charAt(0);
int、double 等基本類型有對應 nextInt()、nextDouble(),因它們是標記化數據的典型代表char 歸屬字元流範疇,String 是字元序列的標準表示String 處理文本,char 僅用於單字元運算混用 nextInt() 後直接 nextLine() 會讀到空行,需額外呼叫一次 nextLine() 清空緩衝區。字符輸入建議統一用 next() 配合 charAt(0)。