题目描述
•连续输入字符串,请按长度为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();
}
}
}
}
}