分情况讨论,主要利用StringBuffer和substring方法,空间占用较大。
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str;
while(sc.hasNext()){
str = sc.nextLine();
strTo8(str);
}
}
public static void strTo8(String str){
int len = str.length();
StringBuffer sb;
if(len == 8){
System.out.println(str);
}
else if(len == 0){
return;
}
else if(len < 8){
sb = new StringBuffer(str);
while (sb.length()!=8){
sb.append('0');
}
System.out.println(sb.toString());
}
else if(len >8){
String str2;
int a = len % 8;
for(int i=0;i<len-a;i+=8){
str2 = str.substring(i,i+8);
System.out.println(str2);
}
if(a>0){
str2 = str.substring(len-a,len);
sb = new StringBuffer(str2);
while (sb.length()!=8){
sb.append('0');
}
System.out.println(sb.toString());
}
}
}
}