打印正向排序,然后向后排序直至开始?

Are*_*efe 0 java recursion

我有一个有趣的问题要解决,我已经给startend整数值,我需要从打印startend,然后从endstart使用递归.

例如 -

start = 2和end = 5然后该方法应该打印以下内容,

2,3,4,5,4,3,2
Run Code Online (Sandbox Code Playgroud)

我可以使用代码轻松完成第一部分,

public static void countUp(int start, int end) {    

    System.out.println(start);

    if(start< end){

        countUp(start+1, end);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,然后在递归中增加起始值,我没有办法找到减少的位置.如何改进我的代码只提一种方法可以使用?目前,它只是打印

2,3,4,5 // I don't care about the commas  
Run Code Online (Sandbox Code Playgroud)

roo*_*ler 6

试试这个,

public static void countUp(int start, int end) {    

 System.out.println(start);

  if(start< end){

     countUp(start+1, end);
     System.out.println(start); //backtracking

   }
}
Run Code Online (Sandbox Code Playgroud)