1问题描述
连续输入一串字符串(长度不超过100),请按字符串长度为10拆分每个字符串后,将拆分的字符串输出到新的字符串数组;
长度不是10整数倍的字符串请在后面补字符@,空字符串不用处理。
输入格式
连续输入字符串,字符串长度小于100
输出格式
输出到长度为10,的新字符串数组,打印新字符串数组值
2方法
(1)设置一个临时空字符串temp和一个整数j;每遍历字符串s中的一个字符,j++,temp=temp+s.char;当整数j=10时,j=0,temp=“”
注:可以直接打印结果,不需要list或string数组
3具体实现
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String s=scanner.nextLine();
if (s==null ||s.trim()==""){
System.out.println("输入的字符不合要求");
}
int j=0;
List<String> list=new ArrayList<>();
String temp="";
for (int i=0;i<s.length();i++){
if (j>9){
j=0;
list.add(temp);
temp="";
}
++j;
temp+=s.charAt(i);
}
int n=temp.length();
while (n>0&&n<10){
temp+="@";
n++;
}
if (temp.length()==10){
list.add(temp);
}
System.out.println(list);
}