题解 | #字符串分隔#

字符串分隔

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);
        }

    }
}

如代码所写

全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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