forked from Doragd/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
字符串字符串字符串
Description
- 输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
- 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
- 问题分解:
- 先判断是否为8的整数倍 不是先将0填充满足条件
- 然后在分割 注意subString这个方法直接取的 beginIndex包括 endIndex不包括 是个坑
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String inputStr = in.nextLine();
if(inputStr == null){
return;
}
int length = inputStr.length();
int splitLength = 8;
int mod = length % splitLength ;
//重新思路 首先判断是否取模为整
//1. 不为整的话 不足的直接凑整在分割
if(mod != 0){
StringBuilder sb = new StringBuilder(inputStr);
for(int i = 0 ; i < 8-mod ;i++){
sb.append("0");
}
inputStr = sb.toString();
}
String outStr = "";
//2. 如果是整 直接分割遍历
for(int i=0 ;i < length; i += splitLength){
outStr = inputStr.substring(i , i + splitLength);
System.out.println(outStr);
}
}
}Metadata
Metadata
Assignees
Labels
字符串字符串字符串