两个字符串的值怎样对应项值相加?想了几个小时了!
题目意思如下
已知某字符串数组,包含如下初始数据:a1,a2,a3,a4,a5 已知另一字符串数组,包含如下初始数据:b1,b2,b3,b4,b5,做程序将该两个数组的每一对应项数据相加存入另外一个数组,并输出。输出结果为:a1b1,a2b2,a3b3,a4b4,a5b5。
我自己做了几个小时,
String[] s1={"a1","a2","a3","a4","a5"};
String[] s2={"b1","b2","b3","b4","b5"};
String[] s3=new String[s1.length];
String c=null;
for(int i=0;i<s1.length;i++)
{
for(int j=0;j<s2.length;j++)
{
c=String.valueOf((s1[i]+s2[j]));
System.out.print(c);
}
}
改来改去,还是错误的。求高手指点下
------解决方案--------------------修改:记得节分啊
String[] s1={"a1","a2","a3","a4","a5"};
String[] s2={"b1","b2","b3","b4","b5"};
String[] s3=new String[s1.length];
String c=null;
for(int i=0;i<s1.length;i++)
{
c=String.valueOf((s1[i]+s2[i]));
System.out.print(c);
}
------解决方案--------------------c=String.valueOf((s1[i]+s2[i])); 之后 s3[i] = c;
------解决方案-------------------- s3[k]=String.valueOf((s1[k]+s2[k]));
System.out.print(s3[k]+",");