I can print it using nested loop, while loop and so forth and i'm having a challenge trying to print it using one for loop only.
Here's what i have so far and it works fine. I want to use string concatenation for *
String stars = " ";
for(int i = 1; i <= 7; i++;)
stars = stars + "*\n**\n***\n****\n*****\n******\n*******";
Is there a way to add to have the loop do the * rather than writing it like i did.
Amir Ahmed wrote:Is there a way to add to have the loop do the * rather than writing it like i did.
Is there a way? Yes, I came up with 3. The ways I came up with (in addition to yours) include:
1) Using an array of Strings
2) Using the Arrays.fill(char, char) method
3) Using a Format String
I am sure there are other ways.
Here's what i have so far and it works fine.
Surely that produces the pattern 7 times over.
There is a way to do it with concatenation but you need to use 2 separate Strings, one to hold the growing line of asterisks and one to concatenate each line of asterisks to.
Amir Ahmed wrote:Hello Steve, thanks for your response. As i'm really new to java, i was wondering if there is any way you can write the code for your suggestions because im having a hard time doing so. Thanks again!
In the future, please UseTheForumNotEmail (or mooseages), thanks. We are NotACodeMill here at the ranch. We will try to help you but won't give you the code. I gave some hints on the first three ideas that came to mind, and Tony gave another great idea. Pick one and try to implement it. Then we can help you with specific questions you might have.