描述
•連續輸入字符串,請按長度為8拆分每個輸入字符串並進行輸出;
•長度不是8整數倍的字符串請在后面補數字0,空字符串不處理。
(注:本題有多組輸入)
輸入描述:
連續輸入字符串(輸入多次,每個字符串長度小於等於100)
輸出描述:
依次輸出所有分割后的長度為8的新字符串
示例1
輸入:
abc 123456789
輸出:
abc00000 12345678 90000000
1 import java.io.*; 2 3 public class Main{ 4 public static void main(String[] args) throws IOException { 5 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 String str; 7 while((str = br.readLine()) != null) { 8 if(str.length() % 8 != 0) { //長度不是8的整數,后面補零 9 str = str + "00000000"; 10 } 11 while (str.length() >= 8) { //前面已經對字符串判斷,要么是8的整數倍,要么后面補了0 12 System.out.println(str.substring(0,8)); //顯示字符串前8個字符 13 str = str.substring(8); //截取字符串,從下標為8開始,也就是第9個字符 14 } 15 } 16 } 17 }
