字符串分隔
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
连续输入字符串(输入2次,每个字符串长度小于100)
输出到长度为8的新字符串数组
[java] view plain copy <span style="font-size:18px;">import java.util.*; public class Main{ public static void main(String[] args) { String[] strs=new String[2]; Scanner scan=new Scanner(System.in); strs[0]=scan.nextLine(); if(strs[0].length()==0 || "".equals(strs[0])) { return; } else { strs[1]=scan.nextLine(); getStrs(strs[0]); getStrs(strs[1]); } } static void getStrs(String str) { int m=str.length()/8; int n=str.length()%8; while(m>0) { System.out.print(str.substring(0, 8)); str=str.substring(8); System.out.println(); m--; } System.out.print(str); for(int j=0;j<8-n;j++) { System.out.print("0"); } System.out.println(); } }</span> 方法二:
[java] view plain copy <span style="font-size:18px;">import java.util.*; public class ZiFuChuan { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String one = sc.nextLine(); String two = sc.nextLine(); ret(one); ret(two); } private static void ret(String str) { do { if(str == null || "".equals(str)) { return; } String sub = ""; if (str.length() < 8 && !"".equals(str)) { String a = rightZero(str); System.out.println(a); break; } else { sub = str.substring(0, 8); System.out.println(sub); str = str.substring(8, str.length()); } } while (str.length() > 0); } private static String rightZero(String str) { int len = 8 - str.length(); for (int i = 0; i < len; i++) { str = str + "0"; } return str; }</span>