定义变量a每次增加8,就是8的倍数,然后判断ch[i]是否超出范围,超出的话就输出0。#牛客春招刷题训练营#
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String s = in.next(); char[] ch = s.toCharArray(); int a = 0; while (a < s.length()) { for (int i = a; i < a + 8; i++) { if (i < s.length()) { System.out.print(ch[i]); } else { System.out.print('0'); } } System.out.println(); a += 8; } } }