//定义字符串缓冲流
StringBuilder sb = new StringBuilder();
//定义变量初始化值
int x = 0;
//例如 字符长度 3 字符长度 4 满足其中条件 不满足结束
while (x<s1.length() || x<s2.length()) { //例如输入的字符串: a b c x y z n
// x< 3
if (x<s1.length()) {
sb.append(s1.charAt(x)); //满足条件 添加 a b c 不满足结束
} // x<4 //a + x + b + y + c + z + n
if (x<s2.length()) {
sb.append(s2.charAt(x)); //满足条件 添加 x y z n 不满足结束
}
x++;
}
//打印输出
System.out.println(sb);
public class Demo {
public static void main(String[] args) {
String a = "abcdef";
String b = "123"; //调用静态方法,并打印返回结果
System.out.println(sumAB(a,b));
}
//定义静态方法,需要参数为两个字符串,返回结构为字符串形式
public static String sumAB(String a , String b){
//定义空串,接收每次循环的结果
String ab = "";
//循环遍历长度较短的字符串,其中任意一个为空串,此循环会被跳过
for(int i = 0; i<a.length()&&i<b.length();i++){
ab = ab+a.charAt(i)+b.charAt(i);
}
int len = ab.length()/2;
//累加上剩下的字符串
return ab+a.substring(len)+b.substring(len);
}
}