another for loop question
I thought I knew how loops worked drawing memory until I got this assignment with JOptionPane. Please read the following.
Use one and only one for loop to print the following pattern in one dialog box. Do not use nested for loops. Use only one for loop, not two or more. Do not use any other kind of loop. Do not use a switch/case statement or if conditions. The same code should work for 7 lines of asterisks or 17 lines of asterisks, simply by changing the number of times the loop executes, from 7 to 17.
*
**
***
****
*****
******
*******
This is what I have put down
String message = "";
int i;
for(i = 0; i < 7; i++)
{
message += "*";
JOptionPane.showMessageDialog(null, message);
}
putting JOptionPane inside of the loop will get you 7 JOption boxes with the asterisk incrementing each time. If I put it outside of the loop I will get on box, but 7 asterisks in a row. Now adding a \n to the end of the asterisk will give me 7 asterisks in a row of 1 and a column of seven. How in the world do I get this asterisk to increment in one JOption box? Hints, programing logic, sites, anything that can help is much appreciated!
trial 2.
String message = "";
int i;
for(i = 0; i < 7; i++)
{
message += "*";
}
JOptionPane.showMessageDialog(null, message);
you get one box
*******
trial 3
String message = "";
int i;
for(i = 0; i < 7; i++)
{
message += "*\n";
}
JOptionPane.showMessageDialog(null, message);
*
*
*
*
*
*
*
I have tried all sorts of things, but when I write out the memory on a piece of paper, I get stuck. If this was command line, this wouldn't be an issue!
Thank You for your time,
Re: another for loop question
The job is to build a String with lineend characters at the right places to put the number of *s on each line that are needed to make the display.
Use a counter to determine when a lineend should be added.
Re: another for loop question
I am not quite sure if I follow you norm. in this case, i is the counter, using i++ in the for loop will only bump up the count. For a better understanding I showed i in the count of
message += i + "*\n";
output
1*
2*
3*
4*
5*
6*
7*
I get one JOption box, which is good. not understanding why the for loop won't add the incrementing *. For loop repeats 7 times, each time the loop sees *\n. Okay that is wrong, how do I add in a return, and still get the * to increment without using a nested forloop?
Re: another for loop question
Add a "*" every time around the loop
Add a "\n" when the counter says to add the next newline.
To see when to add the newline, take a piece of paper and write the design of *s in the rows they should go in and then count the *'s starting at the beginning:
1
23
456
etc
you need a newline after 1, 3, 6 etc
Find the formula for computing when you need to add the newline.