0
如果我將文本更改爲兩個單詞,程序將不會輸出任何內容。不知道如何解決這個問題,提前感謝。數組不會使用小句子
public class test {
public static void main(String args[]) {
String text = "The cat sat on the mat!"; //Change the string to "Hello there!"
int wordLengthCount [] = new int [20];
String wordCountText = "";
String sentence[] = text.split("[,\\-:\\?\\!\\ ]");
for (int i = 0; i < sentence.length; i++)
{
wordLengthCount[sentence[i].length()]++;
}
for(int wordLength=0; wordLength<sentence.length; wordLength++)
{
if (wordLengthCount[wordLength] != 0){
wordCountText += wordLengthCount[wordLength] + " with length of " + wordLength + "\n";
}
}
System.out.println(wordCountText);
}
}
WFM,輸出兩行: 「1具有2長度」 和「5長度爲3「,現場證明:http://ideone.com/cl4o5V – 2014-03-05 18:25:11
嘗試將字符串更改爲」Hello there!「將不會有輸出。謝謝 – Joe