【问题标题】:Recursion output not as expected递归输出不如预期
【发布时间】: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


【解决方案1】:
Relevant statement                                      Output
=================================================       ======================
mysteryMix("abcdefgh!")
  System.out.println("i entered loop");                 i entered loop
  System.out.println(str);                              abcdefgh!
  System.out.println(str);                              abcdefgh!
  System.out.println("I entered if statement");         I entered if statement
  mysteryMix("abc")
    System.out.println("i entered loop");               i entered loop
    System.out.println(str);                            abc
    System.out.println(str);                            abc
    System.out.println("I entered if statement");       I entered if statement
    mysteryMix("a")
      System.out.println("i entered loop");             i entered loop
      System.out.println(str);                          a
    System.out.println(str);                            abc
    System.out.println(...);                            FOR REAL PRINT: b
    System.out.println("length "+length);               length 3
    System.out.println(length/3);                       1
    System.out.println(2*length/3);                     2
    mysteryMix("c")
      System.out.println("i entered loop");             i entered loop
      System.out.println(str);                          c
  System.out.println(str);                              abcdefgh!
  System.out.println(...);                              FOR REAL PRINT: def
  System.out.println("length "+length);                 length 9
  System.out.println(length/3);                         3
  System.out.println(2*length/3);                       6
  mysteryMix("gh!")
    . . . and so forth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-05
    • 2021-03-07
    • 1970-01-01
    • 2018-08-19
    • 2017-04-02
    • 2017-11-20
    • 2015-01-17
    • 2016-06-20
    相关资源
    最近更新 更多