2024-10-31 14:31:27
public static void main(String[] args) {
//英语句子
String str = "We are only separated by a turned distance, but could not meet.";
//去除特殊符号
str.replace(",","");
str.replace(".", "");
//以空格切分字符串
String[] word = str.split(" ");
//将切分的字符串的数组的第一个单词的长度赋值给max
int max = word[0].length();
//设长度最长单词为maxWord
String maxWord = "";
//遍历切分好的字符串数组
for (int i = 1; i < word.length; i++) {
//判断第二个单词及之后的所有单词的长度的值是否比max大
if(word[i].length()>max) {
//如果比max大,则赋值给max,此时max的值为word[i]
max = Math.max(max, word[i].length());
//此时的单词为
maxWord = word[i];
}
}
System.out.println(maxWord);
}
以我目前的能力只能写成这样,望采纳!
谢谢你了