题解 | 字符串分隔

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.nextLine();
	    //根据长度算出分割的次数
        int count = str.length() % 8 == 0 ? str.length() / 8 : str.length() / 8 + 1; 
        for (int i = 1; i <= count; i++) {
            int max =  i == count ? str.length()  : i * 8;
            StringBuffer buffer=new StringBuffer(str.substring( (i - 1) * 8, max));
            buffer.append("0000000");//最多补7个0
            System.out.println(buffer.substring(0,8));//将多的0分割
        }
    }
}

全部评论
学习了,感谢分享
点赞 回复 分享
发布于 2024-12-27 17:57 四川

相关推荐

02-25 19:38
门头沟学院 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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