Java 题目 连续输入字符串,请按长度为8拆分每个输入字符串并进行输出;

描述

•连续输入字符串,请按长度为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 }

 

 

posted @ 2022-02-13 18:43  海漠  阅读(848)  评论(0编辑  收藏  举报