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