Results 1 to 4 of 4
- 09-24-2011, 05:26 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Two triangles of stars next to each other
All right, I have a problem with printing the next drawing:
Two triangles (height 6 and width 6) next to each other and a space between every asterix.Java Code:* * * * * * * * * * * * * * * * * * * * * (x2)
I know the code for the first triangle and I know I have to use if and else to print the blanks, the problem is I don't know how to write it down propely.
Now I know that for the first row (i) we have two columns with a asterix, namely the first one and the seventh one. The difference between them is 6 blanks. On the second row we have 4 asterixs and the difference becomes 5 blanks. So for every row we have, we get two asterix and we lose one blank until we have the last row with 12 asterix.Java Code:public class Triangles { public static void main(String[] args) { for (int i = 1; i <=6; i++) //for loop repeats 6 times { for (int j = 1; j<=i; j++) //while j <=i, j becomes j+1 { System.out.print(" * " ); //print asterix } System.out.println(); //print result is the first triangle } } }
Any hints how to write this down using if and else inside the for loops?Last edited by Aero; 09-24-2011 at 05:29 PM. Reason: Wrong look
-
Re: Two triangles of stars next to each other
You could use your current code, but will need to add some spaces in between the triangles on each line. I would do something like so:
Java Code:// this makes a new line for (int i = 1; i <= 6; i++) { // this prints triangle * for one row for (int j = 1; j <= i; j++) { System.out.print(" * "); } // print spaces. How many -- you've got to figure out! for .... // code for you to add // print stars for one row. You know how to do this already. for ... // code for you to figure out and add. System.out.println(); }
So get a pen and paper and figure out how many blanks you need and what should be the best parameters to use in the middle for loop. :)
A hint: In your middle loop, I would have it print the exact number of spaces as you currently print for your star print, i.e.,
If you do it this way, the math works out nice and pretty. You'll just need to print more of this when i is small and less when i is bigger, and that's what the for loop is for.Java Code:System.out.print(" "); // three spaces to take the place of space-star-space.Last edited by Fubarable; 09-24-2011 at 05:41 PM.
- 09-24-2011, 06:31 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Re: Two triangles of stars next to each other
This works perfect. Thanks a lot! Though I wonder how this problem can be solved with if and else. Do you have to declare variables outside your loop and then after the first triangle is made, then compare it with the rows i, print spaces, else print a star?Java Code:public static void main(String[] args) { for (int i = 1; i<=6; i++) //makes a new row { for (int j = 1; j<=i; j++) //makes a new column { System.out.print(" * " ); //print triangle 1 } for (int s = 5; s>=i; s--) //makes spaces while there are more spaces then rows { System.out.print(" "); //print spaces } for (int j = 1; j<=i; j++) //makes a new column { System.out.print(" * "); //print triangle 2 } System.out.println(); //print de rows accordingly to the loops } } }
In other words:
Java Code:int spaces = 5; for (int i = 1; i<=6; i++) //makes a new row { for (int j = 1; j<=i; j++) //makes a new column { System.out.print(" * " ); //print triangle 1 } if (spaces >=i) //more spaces than rows { System.out.print(" "); //print spaces } else { System.out.print(" * "); } System.out.println();Last edited by Fubarable; 09-24-2011 at 06:42 PM. Reason: quote tags changed to code tags
- 09-26-2011, 07:20 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Making my yellow stars bigger
By IAM in forum New To JavaReplies: 2Last Post: 12-10-2010, 09:44 PM -
stars
By nalinda in forum New To JavaReplies: 3Last Post: 08-25-2009, 11:48 AM -
Blinking stars
By jholtt23 in forum New To JavaReplies: 0Last Post: 02-19-2009, 05:38 AM -
Need help coding a rhombus in stars
By LinxuS in forum New To JavaReplies: 5Last Post: 10-18-2008, 12:12 AM -
Triangles
By CodeDog in forum New To JavaReplies: 9Last Post: 10-14-2008, 09:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks