字符串方法输出不明白

时间:2016-05-27 23:14:53

标签: 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);
     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;
}
}

输出:

D
ELIVE
R
E
LIV
E
L
I
V
DEVILER

谢谢!

3 个答案:

答案 0 :(得分:0)

对于这段代码

     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

Final Chunk

     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 + serios(s2) + s3这是递归,一个在其自身内部调用的函数。

此递归将一直运行,直到满足if条件。最后打印出DELIVER

您可以看到更好理解的模式。

运行代码时,

DELIVER会像D ELIVE R一样打印出来。第一个和最后一个字母与单词的中心分开。

return s1 + serios(s2) + s3;

因为s2 = ELIVE它将等于s。它将使用子字符串拆分,就像DELIVER一样,成为E LIV E设置

LIV = s2

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

L I V

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

答案 1 :(得分:0)

除了subString正在做什么之外,我认为你的问题是关于系列方法的递归行为。

首先打电话给你发送&#34; DELIVER&#34;。

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

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

 else
      return s1 + serios(s2) + s3;

你可以考虑运行系列(&#34; ELIVE&#34;);对于同样的过程,你会看到这个时间s2将得到&#34; LIV&#34;递归不会再次发生,如果部分将运行。

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

我希望这对你有所帮助。

答案 2 :(得分:0)

对于此类任务,有助于跟踪方法调用

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