public static void recursion(int lo, int current, int hi){
if(current != hi){
System.out.printf("%d ", current);
recursion(lo, current+1, hi);
}
System.out.printf("%d%c", current, current == lo ? '\n' : ' ');
}
This method can print out an string of numbers like yours. It needs the starting point (lo), the current position in the list(current), and the maximum number(hi).
The recursive base case(when we don't want to use recursion anymore) is when current == hi. We print out the number, then return.
All other values print current, recurse, then print again after recursion is done.
This solution has the added benefit of printing a space after each char except after the final number. No trailing space :)
Initial invocation of this method would be like so:
recursion(0, 0, 5); //prints 0 -> 5 -> 0
recursion(0, 5, 5); //prints 5 -> 0
recursion(0, 1, 5); //prints 1 -> 5 -> 0
recursion(0, 6, 5); //counts up forever and eventually crashes
Some helpful method to make calling simpler for common uses, you could do something like this:
public static void countUpFromZeroDownToZero(int max){
recursion(0, 0, max);
}
public static void countFromXToYToX(int lo, int hi){
recursion(lo, lo, hi);
}