我希望你对某事有所了解。我已经编程了大约一年了,而且我觉得我在我的程序中留下的评论中的太描述性。所以我打算给你一个我编写的小程序,我希望你能给我一些反馈:)
import java.util.Scanner;
public class ListWordsInString {
public static void main(String[] args) {
Scanner stdin = new Scanner( System.in );
String str;
String word;
System.out.println("Enter a sentence: ");
str = stdin.nextLine();
str = str.toLowerCase();
// The following code goes through the string entered by the user (str)
// until it reaches a word. A word is considered any grouping of letters
// and apostrophes. For example: (that's, 'round, truck, etc.)
// Then it adds the current and the following characters of the string (str)
// in a newly created one called "word". It stops adding characters when
// it reaches an illegal one (that'd be one that is NOT a letter OR a apostrophe)
// or the end of the string. Then it prints the word and starts to search for another one.
for (int i = 0; i < str.length(); i++) {
word = "";
while (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' || str.charAt(i) == '\'') {
word += str.charAt(i);
i++;
if (i == str.length()) {
break;
}
}
if (!word.equals("")) {
System.out.println(word);
}
} // for()
} // main()
}
P.S:英语不是我的第一语言,对于任何拼写或语法错误都很抱歉。
答案 0 :(得分:1)
Javadoc是你的朋友,拥抱它。