菜鸟求助!
初学者,自己有琢磨了一下!
但是也不是很懂!
希望高手帮忙下面的编程
/**
* Count the number of occurrences of a particular word in a given string.
* For example, wordCount("The damsel then ate the dragon.", "the") returns 1.
* [Neither "The" nor "then" match "the", because capital letters are
* different from lower case letters and only whole words match.]
*
* @param fullString the string in which occurrences of word are to be counted
* @param targetWord the word whose occurrences are to be counted
* @return the number of times "word" occurs as a separate word
*
* NOTE: This method should make use of the StringTokenizer class.
*/
/*
* HINTS:
* A while loop is most appropriate for this method.
* (while loops are used when you do not know in advance how many times a loop will repeat)
*
* StringTokenizer is another class in the project. Open it and note
* that it has TWO methods. If you're unsure how to use them, try creating
* a StringTokenizer object manually and calling methods on it to see how it
* works.
*
* Remember when you compare two Strings you must use .equals
*/
public static int wordCount(String fullString, String targetWord) {
return 0;// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
不太明白这里的要求!
高手帮忙!
初学者,自己有琢磨了一下!
但是也不是很懂!
希望高手帮忙下面的编程
/**
* Count the number of occurrences of a particular word in a given string.
* For example, wordCount("The damsel then ate the dragon.", "the") returns 1.
* [Neither "The" nor "then" match "the", because capital letters are
* different from lower case letters and only whole words match.]
*
* @param fullString the string in which occurrences of word are to be counted
* @param targetWord the word whose occurrences are to be counted
* @return the number of times "word" occurs as a separate word
*
* NOTE: This method should make use of the StringTokenizer class.
*/
/*
* HINTS:
* A while loop is most appropriate for this method.
* (while loops are used when you do not know in advance how many times a loop will repeat)
*
* StringTokenizer is another class in the project. Open it and note
* that it has TWO methods. If you're unsure how to use them, try creating
* a StringTokenizer object manually and calling methods on it to see how it
* works.
*
* Remember when you compare two Strings you must use .equals
*/
public static int wordCount(String fullString, String targetWord) {
return 0;// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
不太明白这里的要求!
高手帮忙!
return 0;// 在这里写你的代码
}
int count = 0; StringTokenizer st = new StringTokenizer(fullString); while (st.hasMoreTokens()) {
if (targetWord.equals(st.nextToken())) {
count++;
}
continue;
} return count;
}