【发布时间】:2019-08-28 12:41:57
【问题描述】:
有没有人能够弄清楚这个递归代码是如何工作的以及为什么它会输出它所做的事情?
也许你可以包括它所经历的步骤来得到它的输出?太感谢了!附上代码...(它输出 bdefh)。
另请注意,我是初学者。
public class difficult {
public static void main(String[] args) {
mysteryMix("abcdefgh!");
}
public static void mysteryMix(String str) {
System.out.println("i entered loop");
System.out.println(str);
int length=str.length();
if(length>=3)
{
System.out.println(str);
System.out.println("I entered if statement");
mysteryMix(str.substring(0,length/3));
System.out.println(str);
System.out.println("FOR REAL PRINT: " +str.substring(length/3,2*length/3));
System.out.println("length "+length);
System.out.println(length/3);
System.out.println(2*length/3);
mysteryMix(str.substring(2*length/3));
}
}
}
【问题讨论】:
-
我正在尝试附加它
-
欢迎来到 SO。 A. 类似的question of yours 已被标记为重复。 B. 为了让你的代码和发布更容易理解,请格式化代码,在你的测试中使用大写。 C.“它输出 bdefh”不能正确用于发布的代码。 D. 关注Java Naming Conventions。
标签: java string recursion methods substring