|
-
January 17th, 2009, 10:13 PM
#1
Nested Loops Triangle Pattern Help
I'm trying to print this below pattern using nested loops in Java, but I'm having trouble getting it into the way it's supposed to look.
Code:
*************
***********
*********
*******
*****
***
*
*
***
*****
*******
*********
***********
*************
There basically are two triangles, one going down and one going up. The first triangle starts out with 13 asterisks and then decrease by 2 each line until it gets to 1 asterisk. The second triangle begins with 1 asterisk and then increase until it reaches 13 asterisks.
This is the code that I have now.
Code:
class hourglass
{
public static void main (String[] arguments)
{
//hourglass shape
int size=13;
int starStartPosition = 0;
int starEndPosition = 13;
String printout;
for( int rows=0; rows<size; rows++)
{
for( int cols=0; cols<size; cols++)
{
if(cols < starStartPosition || cols >= starEndPosition)
{
printout += " ";
}
else
{
printout += "*";
}
}
if(rows <6)
{
starStartPosition++;
starEndPosition--;
}
else
{
starStartPosition--;
starPosition++;
}
System.out.println(printout);
printout = "";
}
}
}
The above code only prints the single asterisk in the middle ONCE, but it should be printed TWICE since the shape is made up of TWO triangles. I am thinking of doing two separate nested loops for each individual triangle, but I have no idea how to do that or fix my current code.
Someone please help!
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|