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

Based on my knowledge no matter which way you do it. It will require some sort of repetition which will require a for loop or a while loop. You'll have to have some sort of loop or counter. I am not aware of a method that does this for you. It's possible you might be able to find one in a third-party library.

Yups, thats right. I have no idea how to do that without looping or recursion...

What about Python inspired iteration?

byte range[] = new byte[5];
for (byte nul : range) {
    System.out.println("do stuff");
}

Create a String constant "*********************************************"
then use substring(0,numAster) to pick the right numbert of *s from the fromt of it.

What about Python inspired iteration?

byte range[] = new byte[5];
for (byte nul : range) {
    System.out.println("do stuff");
}

Uhm, you do realise that that is still a for loop, right? (In your code anyway, what actually gets compiled is an Iterator with a while loop.)

Create a String constant "*********************************************"
then use substring(0,numAster) to pick the right numbert of *s from the fromt of it.

Uhm, a loop would still be needed, though. ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.