• 首先输入数字n,表示要输入多少个字符串。连续输入字符串


    按长度为8拆分每个字符串后输出到新的字符串数组,长度不是8整数倍的字符串请在后面补数字0,空字符串不处理

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int count = 0;
    String[] strings = null;
    int index = 0;
    while (scanner.hasNext()){
    if(count == 0){
    count = scanner.nextInt();
    if(count <= 0 ){
    System.out.println("程序结束");
    return;
    }
    strings = new String[count];
    }else {
    String next = scanner.next();
    strings[index] = next;
    index++;
    if(index == count){
    break;
    }
    }
    }
    calc(strings);
    }

    private static void calc(String[] arr) {
    List<String> list = new ArrayList<>(arr.length);

    for (String s : arr) {
    if(s == null || "".equals(s)){
    continue;
    }
    if(s.length() < 8){
    list.add(s);
    }else {
    int count = s.length() / 8;
    if((s.length() % 8) != 0 ){
    count++;
    }
    for (int i = 0; i < count; i++) {
    String t = s.substring(i * 8 , Math.min(s.length(),(i + 1) * 8));
    list.add(t);
    }
    }

    }
    for (String s : list) {
    if(s.length() < 8){
    int zoreCount = 8 - s.length();
    for (int i = 0; i < zoreCount; i++) {
    s+="0";
    }
    System.out.println(s);
    }else {
    System.out.println(s);
    }
    }

    }
  • 相关阅读:
    10.异常
    9.1 oop习题集合
    9.抽象类和接口
    8.oop-多态
    AngularJs学习笔记二
    浅谈如何坚持计划
    妙味课堂——JavaScript基础课程笔记
    前端学习-试卷
    jquery实战
    boost any
  • 原文地址:https://www.cnblogs.com/dongma/p/13222252.html
Copyright © 2020-2023  润新知