题解 | #字符串最后一个单词的长度#

字符串最后一个单词的长度

http://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da

package stringtest;

import java.util.Scanner;

public class String1 {

public static void main(String[] args) {
    System.out.println("输入字符");
    Scanner scan = new Scanner(System.in);
    // 从键盘接收数据
    String nextLine = scan.nextLine();
    // next方式接收字符串
    System.out.println("nextLine方式接收:");
    int i =nextLine.length()-1;
    int t =0;
    // 判断长度是否超标
    if(nextLine.length()<5000||nextLine.length()> 0) {
        System.out.println("字符串合法");

        while(i> 0 && nextLine.charAt(i) != ' ') {
            t++;
            i--;
            if(t == nextLine.length()-1) {
                System.out.println(t+1);
                return;
            }
        }
        System.out.println(t);
    }


}

}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务