题解 | 字符串分隔

字符串分隔

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

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int count  = 0;
        StringBuilder sb  = new StringBuilder(s);
        if(s.length() %8!=0){
            for(int i = 0;i<8-s.length()%8;i++){
                sb.append(0);
            }
        }
        String result = sb.toString();
        for(char c :result.toCharArray()){
            count++;
            System.out.print(c);
            if(count == 8){
                System.out.println();
                count = 0;
            }
        }
        sc.close();
    }
}

用一个动态字符串来添加0至字符串长度是8的倍数,然后将字符一个个输出,每8个字符后边加一个空格

全部评论

相关推荐

测试糕手手:社会第一课,随便吹牛逼,直接说四个月,别老实。老实人只会被欺负
点赞 评论 收藏
分享
07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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