public class LongWord {
public static void main(String args[]) {
String text = "my brother is taller than [email protected] I always a short man,but smart than him";
// Find the longest word in the String
String[] words = text.split("\\s");
String longestWord = "";
for (int i = 1; i < words.length; i++) {
int firstLen = words[i - 1].length();
int secondLen = words[i].length();
if (firstLen <= secondLen) {
longestWord = words[i];
}
}
System.out
.println("===================================================\nLongest Word:::: \n");
System.out.println(longestWord);
}
}
//這是查找語句中最長單詞的示例程序。所以輸出應該是「me @ 1233334」這個詞。但我得到的是「人,但」字作爲輸出。任何人都可以請幫助我什麼是錯誤的程序。字符串長度()比較不正常
用鋼筆執行你的算法,在peper,寫在每一步中每個變量的值。你有一個基本的邏輯思想。 –
特別關注你正在比較的單詞的長度。 – GhostCat
你可能想看看另一個名爲StringTokenizer的java類。有了它,您可以指定使用哪些分隔符,如空格和逗號。 – Alan