package cn.itcast.test;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
System.out.println("请输入内容,单词之间用空格隔开 如(i love java )");
String str = new Scanner(System.in).nextLine();
//创建一个方法,求出字符串中单词的长度 比较单词字符串大小
lengthOfSplitStr(str);
}
private static void lengthOfSplitStr(String str) {
//i love java 切割字符串,得到一个装着单词的数组
String[] splitStr = str.split(" ");
String maxString = "";
String minString = "";
for (String string : splitStr) {
if (maxString.length() < string.length()) {
maxString = string;
}
if (minString.length() > string.length()) {
minString = string;
}
}
System.out.println("长度最大的单词是:"+maxString);
System.out.println("长度最小的单词是:"+minString);
}
}
|