2012-10-18 144 views
-3

我需要弄清楚如何製作一個程序來統計用戶輸入的句子中的單詞。用戶還輸入每個單詞必須的長度。因此,如果用戶輸入5個字母的單詞,並且該句子包含4個字母的單詞;這個詞不會被計算在內。我需要一個wordcount的程序

這是我的全部......

public class wordcount { 
    public static void main(String[] args) { 
    int length = IO.readInt(); 
    String sentence = IO.readString(); 
    int full = sentence.length(); 
    int wordcount = 0; 

    for(int i = 0; i < length; i++){ 
    } 
    System.out.print(wordcount); 
    } 
} 
+0

這還不夠.... – duffymo

+0

我需要一個直升機 – MadProgrammer

+0

我知道我需要幫助! –

回答

1
return sentence.split("\\s+").length; 
+0

這是不夠的。根據OP,計數時可能需要每個字的特定字符數。 –

+0

@ E_net4是的,但這是一個有趣的開始 – MadProgrammer

+0

@MadProgrammer它可能很有趣簡單,但我們通常更喜歡在答案中有一個完整的解決方案,你不覺得嗎? –

0

這將讀取計數輸入線。

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
    int length = scanner.nextInt(); 
    String sentence = scanner.nextLine(); 
    scanner.close(); 

    int wordcount = 0; 
    String[] words = sentence.split(" "); 
    for(int i = 0; i < words.length; i++){ 
     if(words[i].length() >= length){ 
      wordcount++; 
     } 
    } 
    System.out.print(wordcount); 
} 

}

相關問題