题解 | #字符串分隔#

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        while (str.length() > 8) {
            //因为是要8的倍数,所以到7就可以了
            System.out.println(str.substring(0, 8));
            //将原字符串截断
            str = str.substring(8);
        }
        if (str.length() == 8) {
            System.out.println(str);
        } else {
            //不能直接使用str.length(),因为str的长度会不断变化
            int n = str.length();
            for (int i = 0; i < 8 - n; i++) {
                str = str + "0";
            }
            System.out.println(str);
        }

    }
}

如代码所写

全部评论

相关推荐

职场水母:为啥你们整简历都喜欢整一大堆没用的,是期待让hr觉得很多,自己很厉害吗
0offer是寒冬太冷还...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务