题解 | #字符串分隔#

字符串分隔

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        StringBuilder batch;
        int index = 0;
	  /*
	   * 若接下去截取的长度大于等于8,则说明不需要补位
	   */
        while (index < str.length()) {
            if (str.substring(index).length() >= 8) {
                batch = new StringBuilder(str.substring(index, index + 8));
                System.out.println(batch);
            } else {
                batch = new StringBuilder(str.substring(index));
                for (int i = 0; i < 8 - (str.length() - index); i++) {
                    batch.append("0");
                }
                System.out.println(batch);
            }
            index += 8;
        }
    }
}

全部评论

相关推荐

巧克力1:双选会不如教室宣讲会
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务