HELP WITH FOR LOOPS! trying to concatenate without using string builder
I am trying to make this outcome: basically adding an asterisk more each line.
*
**
***
****
*****
******
*******
but i dont want to use this code:
*\n**\n***\n****\n*****\n etc.
so far my code looks like this:
message = "";
String stars = "*";
for (int i=1; i<8; i++)
{
message = message + stars + "\n";
}
JOptionPane.showMessageDialog(null, message);
Re: HELP WITH FOR LOOPS! trying to concatenate without using string builder
Quote:
Originally Posted by
janeeatsdonuts
I am trying to make this outcome: basically adding an asterisk more each line.
*
**
***
****
*****
******
*******
but i dont want to use this code:
*\n**\n***\n****\n*****\n etc.
so far my code looks like this:
message = "";
String stars = "*";
for (int i=1; i<8; i++)
{
message = message + stars + "\n";
}
JOptionPane.showMessageDialog(null, message);
Great.
Now do you have an actual question that we can answer?
Re: HELP WITH FOR LOOPS! trying to concatenate without using string builder
uhm.
the output for my code is:
*
*
*
*
*
*
*
instead of
*
**
***
****
*****
******
*******
i want to make
*
*
*
*
*
*
*
into
*
**
***
****
*****
******
*******
how do i make it into:
*
**
***
****
*****
******
*******
Re: HELP WITH FOR LOOPS! trying to concatenate without using string builder
Figure out the logic first. I would do something like:
Code :
On row 0 I want 1 star
On row 1 I want 2 stars
on row 2 I want 3 stars
...
on row x I want (x + 1) stars
...
stop at row y.
Then translate that into pseudo code
Code :
begin loop that will loop y times.
print (loop index + 1) stars
print new line
end loop
That line "print loop index + 1 stars can be further broken down into its own loop.
Code :
begin loop that will loop y times.
// print (loop index + 1) stars
create an inner loop that loops from 0 to the current (outer loop's index + 1)
print a "*"
end inner loop
print new line
end loop
Now you try to translate the pseudo-code into Java code.