Java 字符串方法输出不理解

Java 字符串方法输出不理解,java,Java,从下面的问题中,我不明白输出是如何来的。有人能解释一下它是怎么来的吗 public class mystery{ public static void main(String[] args) { System.out.println(serios("DELIVER")); } public static String serios(String s) { String s1 = s.substring(0,1); System.out.println(s1)

从下面的问题中,我不明白输出是如何来的。有人能解释一下它是怎么来的吗

public class mystery{
public static void main(String[] args) {

    System.out.println(serios("DELIVER"));

}



public static String serios(String s)
{
     String s1 = s.substring(0,1);
     System.out.println(s1);
     String s2 = s.substring(1, s.length() - 1);
     System.out.println(s2);
     String s3 = s.substring(s.length() - 1);
     System.out.println(s3);
     if (s.length() <= 3)
          return s3 + s2 + s1;

     else
          return s1 + serios(s2) + s3;
}
}
谢谢

对于这段代码

     String s1 = s.substring(0,1);//this initializes s1 = D as Substring reads up to right before the ending index which is 1.
     System.out.println(s1);//print s1
这块

     String s2 = s.substring(1, s.length() - 1);//Starts where the previous chunk left off, ends right before the ending initializing s2 = ELIVE
     System.out.println(s2);//print s2
最后一块

     String s3 = s.substring(s.length() - 1);//This chunk starts from the end and captures R
     System.out.println(s3);//print s3
这三个区块及其打印语句将为您提供

D ELIVE R
现在让我们继续

最后的return语句返回s1+serioss2+s3。这是递归,一个在自身内部调用的函数

此递归将一直运行,直到满足if条件。终于打印出来了

您可以看到一种模式,以便更好地理解

当运行时交付代码按如下方式打印。第一个字母和最后一个字母与单词中心分开

return s1 + serios(s2) + s3;
由于s2=ELIVE,它将变为等于s。它将使用子字符串进行拆分,就像交付成为生活环境一样

s现在将等于LIV,并被拆分为

L I V

最后,s的长度等于3,因此if条件将运行并输出DEVILER

除了子字符串所做的事情之外,我认为您的问题与series方法的递归行为有关

第一次给你打电话,然后送过去

在下面的一行中,您可以看到输入参数是否大于3,该方法这次使用s2再次调用自身。对于第一次迭代,s2=ELIVE

你可以考虑运行seriesELIVE;对于同一个过程,你会看到这次s2将得到LIV,递归不会再次发生,如果部分将运行

 if (s.length() <= 3)
          return s3 + s2 + s1;

我希望这对您有所帮助。

对于这种类型的任务,跟踪方法调用会有所帮助

public class mystery {
    public static void main(String[] args) 
    {
        serios("DELIVER", "");
    }

    public static String serios(String s, String indentation)
    {
        String s1 = s.substring(0, 1);
        System.out.println(indentation + "\"" + s1 + "\" is the substring of \"" + s + "\" at 0");

        String s2 = s.substring(1, s.length() - 1);
        System.out.println(indentation + "\"" +s2 + "\" is the substring of \"" + s + "\" from 1 to " + (s.length() - 2));

        String s3 = s.substring(s.length() - 1);
        System.out.println(indentation + "\"" + s3 + "\" is the substring of \"" + s + "\" at " + (s.length() - 1));

        if (s.length() <= 3)
            return s3 + s2 + s1;
        else 
        {
            indentation += " ";
            return s1 + serios(s2, indentation) + s3;
        }
    }
}

请看一下。在提出问题之前,您应该阅读文档并尝试理解输出。此外,还有一个对serios的递归调用。如果您使用IDE的调试功能,设置断点并观察变量,您可能会看到发生了什么。您不了解代码/输出的哪一部分?你明白为什么第一行是D吗?如果没有,请阅读@Gendarme提供的链接。理解递归方法的常用方法是跟踪其方法调用。看到我的答案了吗
if (s.length() <= 3)
      return s3 + s2 + s1;

 else
      return s1 + serios(s2) + s3;
 if (s.length() <= 3)
          return s3 + s2 + s1;
public class mystery {
    public static void main(String[] args) 
    {
        serios("DELIVER", "");
    }

    public static String serios(String s, String indentation)
    {
        String s1 = s.substring(0, 1);
        System.out.println(indentation + "\"" + s1 + "\" is the substring of \"" + s + "\" at 0");

        String s2 = s.substring(1, s.length() - 1);
        System.out.println(indentation + "\"" +s2 + "\" is the substring of \"" + s + "\" from 1 to " + (s.length() - 2));

        String s3 = s.substring(s.length() - 1);
        System.out.println(indentation + "\"" + s3 + "\" is the substring of \"" + s + "\" at " + (s.length() - 1));

        if (s.length() <= 3)
            return s3 + s2 + s1;
        else 
        {
            indentation += " ";
            return s1 + serios(s2, indentation) + s3;
        }
    }
}
"D" is the substring of "DELIVER" at 0
"ELIVE" is the substring of "DELIVER" from 1 to 5
"R" is the substring of "DELIVER" at 6
 "E" is the substring of "ELIVE" at 0
 "LIV" is the substring of "ELIVE" from 1 to 3
 "E" is the substring of "ELIVE" at 4
  "L" is the substring of "LIV" at 0
  "I" is the substring of "LIV" from 1 to 1
  "V" is the substring of "LIV" at 2