我正在處理遞歸方法,該方法將返回並在我的主方法中打印,每個字符串的三個字母都向後。該字符串是(args [1])。例如,如果字符串是「堆棧」。它應該輸出到:遞歸方法打印字符串中的每個字母3次向後
kkkcccaaatttsss
到目前爲止,我設法向後打印字符串。我應該如何去打印每個字符串三次?
到目前爲止我的代碼:
public static void main(String[] args){
int number = Integer.parseInt(args[0]);
String word = new String("");
word = args[1];
String methodddd = recursive1.method4(word, number);
System.out.println(methodddd);
}
public static String method4(String word){
int length = word.length();
if (length == length*3){
return "";
}
return word.substring(length-1, length) + method4(word.substring(0, length-1));
}
哦好吧,我看到我的基本情況是造成我的麻煩。由於我在調整遞歸情況時繼續出現錯誤。謝謝你的解釋。它真的幫助我理解:) –