题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//通过nextLine()获取输入的一行,如果为空则判断输入的是回车键
String inputs=sc.nextLine();
if(inputs.equals("")){
System.out.print("您输入了回车键");
}else {
String[] splits=inputs.split(" ");
//获取字符串中的最后一个字符串,并计算其长度
String word=splits[splits.length-1];
System.out.print(word.length());
}
}
}
