程序返回一个字符串,得到两个字符串的互换字符;即 string s1 = "aceg" string s2 = "hfdb" 结果是 "abcdefgh"(两个字符串的长度总是相同的;
好吧,我是这样处理的:
public class Eserc2 {
public static String ricorsiveString(String s1, String s2) {
if(s1.length() == 0 && s2.length() == 0) {
return "";
} else {
char c = s1.charAt(0);
char b = s2.charAt(s2.length()-1);
String s = String.valueOf(c) ;
String t = String.valueOf(b) ;
String tot = s+t ;
return tot + ricorsiveString(s1.substring(1),s2.substring(s2.length()));
}
}
public static void main(String[] args) {
String a = "aceg";
String b = "bdfh";
ricorsiveString(a,b);
}
}
问题是当我编译 pc 时发现异常:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at eserc2.Eserc2.ricorsiveString(Eserc2.java:20)
at eserc2.Eserc2.ricorsiveString(Eserc2.java:26)
at eserc2.Eserc2.main(Eserc2.java:34)
你能帮我吗???