0
使用遞歸來反轉字符串的簡單程序。唯一的問題是我運行程序時出現錯誤。遞歸地反轉字符串
import java.util.Scanner;
public class reverseString {
public static String reverseTheString(String s){
if (s.length() == 0)
return s;
return reverseTheString(s.substring(1) + s.charAt(0));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string to be reversed.");
String s = keyboard.nextLine();
reverseTheString(s);
}
}
將's.length()== 0'改爲's.length()== 1' –