题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
遍历字符串,遍历到当前元素时,依次往后找7个元素添加至当前要输出的字符串中即可。如果找不足7个字符,补足0即可。
使用StringBuilder可以较为方便的添加字符,注意下一次循环 i += 8;
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();
for(int i=0;i<str.length();i+=8){
StringBuilder sb = new StringBuilder();
for(int j=i;j<i+8;j++){
if(j > str.length()-1){
sb.append("0");
}else{
sb.append(str.charAt(j));
}
}
System.out.println(sb.toString());
}
}
}
查看25道真题和解析