牛客网字符串分隔java

题目描述

•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

解题思路:1.先考虑长度小于等于8的情况,计算需要的填充数,并打印输出填充。2.循环打印输出所有字符,当长度为8的倍数时(j+1)%8 == 0,输出换行。计算最后一个需要填充的数量,并打印输出填充。

import java.util.Scanner;
public class Main {
    public static void main(String[] arg) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            char ch[] = new char[8];
            if (str.length() <= 8) {
                for (int i = 0; i < str.length(); i++) {
                    ch[i] = str.charAt(i);
                }
                for (int i = str.length(); i < 8; i++) {
                    ch[i] = '0';
                }
                System.out.println(ch);
            }else {
                char ch1[]=str.toCharArray();
                for(int j=0;j<str.length();j++){
                    System.out.print(ch1[j]);
                    if((j+1)%8==0){
                        System.out.println();
                    }
                }
                int count=8-str.length()%8;
                if(count<8) {
                    for (int i = 0; i < count; i++) {
                        System.out.print("0");
                    }
                }
                if (str.length()%8 != 0) {
                    System.out.println();
                }
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值