我有一个反转字符串的递归方法(硬件分配,必须是递归的)。我做到了....但它只在第一次通过后返回字符串的值。通过分析每次通过后的输出,我可以看到它确实正确地完成了它的工作。这是我的代码,以及我在它下面得到的输出:
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