-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Longest word

Luz4年前 (2021-05-10)题库1787
This program reads a line from user's input and analysis each word in the line, prints out each word and the longest length of the words.

FYR: `toUpperCase()` of `String` can turn a string into all upper case, and the `Scanner` has a methods `nextLine()` to read a whole line in.

```Java
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int max = 0;
Scanner in = new Scanner(System.in);
String line = in.@@[nextLine()](1); // read a whole line in
String[] a = line.@@[split(" ")](1); // split the line according to space
for ( @@[String s: a](1) ) { // iterate every word in the line
@@[s = s.toUpperCase()](1); // turn the whole word into upper case
System.out.println(s); // print out the upper case word
max = Integer.max(max, @@[s.length()](1)); // find the longest word
}
System.out.println(max);
in.close();
}
}
```






答案:
第1空:nextLine()

第2空:split(" ")

第3空:String s: a

第4空:s = s.toUpperCase()

第5空:s.length()

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。