我有一个反转字符串(HW分配,必须递归)的递归方法。我做到了......但它仅在第一次传递之后返回字符串的值。通过分析每次通过后的输出,我可以看到它确实做到了正确的工作。继承人我的代码,并输出我得到它下面:为什么不会返回新的字符串?
String s = "Hello, I love you wont you tell me your name?";
int k=0;
public String reverseThisString(String s) {
if(k!=s.length()) {
String first =s.substring(0,k)+s.charAt(s.length()-1);
String end = ""+s.substring(k, s.length()-1);
k++;
s=first+end;
System.out.println(s);
this.reverseThisString(s);
}
return s;
}
输出:
?Hello, I love you wont you tell me your name
是一门功课,因为它是难以扭转的字符串这个坏? – bestsss 2011-02-05 23:06:12