Hello - quick question,
I have created a method which prints a chosen symbol multiple times:
- User enters 5
- Print this:
*****
****
***
**
*
I'm aware of recursion and know how to use it, my method uses it to do the above but I also use another method which contains a for loop to print the symbols. My question is this:
Is there a method that can be called to do the same without the need for a loop? My methods are as below:
// Method to print asterisks in a pyramid formation - number will be the number of asterisks on the bottom
public static int printPyramid( int asterisks )
{
if( asterisks == 0 )
{
return 0;
}
else {
printAsterisks( asterisks );
asterisks--;
return printPyramid( asterisks );
}
}
// Method to print asterisks
public static void printAsterisks( int numAster )
{
for( int i = 1; i <= numAster; i++ )
{
System.out.print("*");
}
System.out.println();
}
Cheers