-
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.
-
January 18th, 2009, 08:22 AM
#2
Re: Nested Loops Triangle Pattern Help
You code does not compile. Check your variable naming. Once corrected works fine. Also when posting next time, and you know you recieve an error, post the error too.
-
January 18th, 2009, 08:24 PM
#3
Re: Nested Loops Triangle Pattern Help
This is interesting, because I can compile the code fine on my computer. It runs and everything. When compiled, it just doesn't look the way it's supposed to look as it's missing one of the middle asterisks.
-
January 18th, 2009, 08:55 PM
#4
Re: Nested Loops Triangle Pattern Help
hint 2: one of these lines posted above is wrong:
Code:
else
{
starStartPosition--;
starPosition++;
}
-
January 19th, 2009, 10:45 AM
#5
Re: Nested Loops Triangle Pattern Help
Originally Posted by Silverwind_4
This is interesting, because I can compile the code fine on my computer.
This is probably because you haven't posted the same code as you are actually compiling and running.
-
January 19th, 2009, 06:23 PM
#6
Re: Nested Loops Triangle Pattern Help
a quicker version:
PHP Code:
public static void main(String[] args) {
int top = 13;
int s = 0;
for (int i=0; i<top*2+1; i++){
s = Math.abs(top-i);
if(s!=0){
for(int j=top; j>s; j--){
System.out.print(" ");
}
for (int j=0; j<s; j++){
System.out.print("* ");
}
System.out.println();
}
}
}
Last edited by Xeel; January 19th, 2009 at 06:30 PM.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
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
|