# Thread: for loop problem. any suggestions?

1. Member
Join Date
Mar 2011
Posts
13
Rep Power
0
I actually did figure it out on my own before his response, but I really appreciate his help. It was good to look and see the solution he came up with vs what I did and realize there are many ways to solve it.

I do actually like it though that you all don't typically spoon feed answers. I know I learn better when I have to figure it out. Some guidance is much appreciated though.

In short, thanks again to all!

Here's what I came up with for my code. It has a ton of documentation notes to myself explaining what I'm doing, so sorry if that makes it hard to read:
Java Code:
```

public class TrianglePrinting //name of program
{
//Main method begins execution of Java program
public static void main(String[] args)
{
//first triangle
for(int i=1; i<=10; i++)/*declare and initialze control
variable; set loop continuation condition; increment control
variable by 1*/
{
for(int j=1; j<=i; j++)/*declare and initialze control
variable; set loop continuation condition; increment
control variable by 1*/
System.out.print("*");//display * to screen
System.out.println(" ");//display white space to screen
}//end for loop
System.out.println("\n");//print new line

//second triangle
for(int i=10; i>=0; i--)/*declare and initialze control
variable; set loop continuation condition; decrement control
variable by 1*/
{
for(int j=1; j<=i; j++)/*declare and initialze control
variable; set loop continuation condition; increment
control variable by 1*/
System.out.print("*");//display * to screen
System.out.println(" ");//display white space to screen
}
//end for loop
System.out.println("\n");//print new line

//third triangle
int nLines = 10;//declare and initialize nLines

for (int i = 1; i <= nLines; i++)/*declare and initialize
control variable; set loop continuation condition; Increment
control variable by 1.*/
{
int nSpaces = i - 1;/*initiate nSpaces to one less than the
numberof lines*/
int nStars = (nLines - i) + 1;/*initiate asterisks to the
number of lines*/

for (int j = 1; j <= nSpaces; j++)/*declare and initialze
control variable; set loop continuation condition; increment
control variable by 1*/
System.out.print(" ");//display white space to screen

for (int j = 1; j <= nStars; j++)/*declare and initialze
control variable; set loop continuation condition;
increment control variable by 1*/
System.out.print("*");//display * to screen
System.out.println("");//display white space to screen
}//end for loop
System.out.println(" \n");//print new line

//fourth triangle
for (int i = 1; i <= nLines; i++)/*declare and initialize control
variable; set loop continuation condition; Increment control
variable by 1.*/
{
int nSpaces = (nLines - i) + 1;/*declare and initialize nSpaces
to one less than the numberof lines*/
int nStars = i - 1;//initiate asterisks to the number of lines

for (int j = 1; j <= nSpaces; j++)/*declare and initialze control
variable; set loop continuation condition; increment control
variable by 1*/
System.out.print(" ");//display white space to screen

for (int j = 1; j <= nStars; j++)/*declare and initialze
control variable; set loop continuation condition; increment
control variable by 1*/
System.out.print("*");//display * to screen

System.out.println("");//display white space to screen
}//end for loop

}//End main method
}//End class```
----jGRASP exec: java TrianglePrinting
*
**
***
****
*****
******
*******
********
*********
**********

**********
*********
********
*******
******
*****
****
***
**
*

**********
*********
********
*******
******
*****
****
***
**
*

*
**
***
****
*****
******
*******
********
*********

2. I am glad you solved it. There will always be more than one way to solve anything. If you wanted you could probably do this in much more complicated ways. The more you work with it, the easier the logic will become. If you get really stuck, come here and ask for help as a last resort. This is how I have been learning java. TBH, I didn't even know how to declare arrays in the beginning of January.

If you are satisfied with the help you have received here, please mark the thread solved with the thread tools at the top.

3. Member
Join Date
Mar 2011
Posts
13
Rep Power
0
Thanks for the encouragement, Sunde887!

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•