通过分割取出最后一个字符
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int rst = maxLengthStr(in.nextLine()); System.out.println(rst); } } public static int maxLengthStr(String source){ if ("".equals(source) || source.length()==0){ return 0; }else{ String[] wordArray= source.split(" "); return wordArray[wordArray.length-1].length(); } } }