[蟲?] 幼稚園算數

看板java作者 (XDDD)時間13年前 (2012/03/21 16:17), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/4 (看更多)
內容 : 算一算每行有幾個字(word)。 Word的定義是連續的字元(letter: A~Z a~z)所組成的字。 輸入說明 : 一段文字(string) 輸出說明 : 字數(int) 範例輸入 : Hello everybody!! This is school principal speeking. 範例輸出 : 2 5 程式碼如下: public class Count { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int wordcount =0; String input = scanner.nextLine(); char[] in_array = input.toCharArray(); System.out.printf("字串長度%d %n", input.length()); for (int i = 0; i < input.length()+1; i++){ if(!Character.isLetter(in_array[i])) wordcount++; System.out.printf("index[%d] = %C %n", i, in_array[i]); } System.out.printf("小朋友輸入%d個單字%n", wordcount); } } 我是將輸入的字串 放到陣列中, 一個一個去比對是不是字母 如果是字母的話 則跳過 否則wordcont+1 但是跑出來卻是錯誤的結果 請問我有哪裡寫錯嗎? 謝謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.33.148.237
文章代碼(AID): #1FQOw5h0 (java)
文章代碼(AID): #1FQOw5h0 (java)