输入字符串但得到一个数字?
Input String but got a number?
我尝试使用递归来反转字符串,但得到的输出是数字
public static String reverse(String str) {
if(str == null) {
return null;
}
if(str.length() > 1) {
int ch = str.charAt(0);
String withoutFirst = str.substring(1);
String reverseNoFirst = reverse(withoutFirst);
String result = reverseNoFirst + ch;
return result;
}
return str;
}
您需要将 ch
声明为 char
,而不是 int
:
char ch = str.charAt(0);
将“int ch = str.charAt(0)”替换为“String chr=String.valueOf(str.charAt(0));”
我尝试使用递归来反转字符串,但得到的输出是数字
public static String reverse(String str) {
if(str == null) {
return null;
}
if(str.length() > 1) {
int ch = str.charAt(0);
String withoutFirst = str.substring(1);
String reverseNoFirst = reverse(withoutFirst);
String result = reverseNoFirst + ch;
return result;
}
return str;
}
您需要将 ch
声明为 char
,而不是 int
:
char ch = str.charAt(0);
将“int ch = str.charAt(0)”替换为“String chr=String.valueOf(str.charAt(0));”