i am working on what should be a relatively simple java recursion problem, though i cannot seem to find a straightforward, one-method solution anywhere.
i am trying to print out asterisks in descending followed by ascending order, so that when the user passes in 3 for example, the print out will look like this:
*
**
***
**
*
EDIT: thanks to the help of @dasblinkenlight this has evolved to:
public void patternMaker(int start, int max, int direction){
if(start == 0){
return;
}
for(int i = 0; i < start; i++){
System.out.print("*");
}
System.out.println();
if(start == max){
direction = -1;
}
patternMaker(start + direction, max, direction);
NOW, it prints the correct amount of asterisks, AND in the proper order:
*
**
***
**
*
thanks everyone for helping!
You need another parameter to tell you which way you are going. You also need to test for the end condition to know when to switch from going up to going down.
It would probably be easier to draw stars before recursing down, although both ways are certainly possible.