题目描述 编写一个函数,输入一行字符,将此字符串中最长的单词输出。
输入仅一行,多个单词,每个单词间用一个空格隔开。单词仅由小写字母组成。所有单词的长度和不超过100000。如有多个最长单词,输出最先出现的。
输入 无 输出 无 样例输入
I am a student
样例输出
student
【AC代码】:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] s1 = s.split(" ");//通过空格把字符分开
int max = 0, t = 0;
for (int i = 0; i < s1.length; i++) {
if (max < s1[i].length()) {
max = s1[i].length();
t = i;
}
}
System.out.println(s1[t]);
}
}