Results 1 to 4 of 4
  1. #1
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Two triangles of stars next to each other

    All right, I have a problem with printing the next drawing:

    Java Code:
    *                
    * *             
    * * *          
    * * * *       
    * * * * *    
    * * * * * * (x2)
    Two triangles (height 6 and width 6) next to each other and a space between every asterix.

    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.

    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
        }
      }
    }
    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.

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.,
    Java Code:
    System.out.print("   "); // three spaces to take the place of space-star-space.
    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.
    Last edited by Fubarable; 09-24-2011 at 05:41 PM.

  3. #3
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Two triangles of stars next to each other

    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
        }
      }
    }
    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?

    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

  4. #4
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Two triangles of stars next to each other

    This thread can be closed also.

Similar Threads

  1. Making my yellow stars bigger
    By IAM in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 09:44 PM
  2. stars
    By nalinda in forum New To Java
    Replies: 3
    Last Post: 08-25-2009, 11:48 AM
  3. Blinking stars
    By jholtt23 in forum New To Java
    Replies: 0
    Last Post: 02-19-2009, 05:38 AM
  4. Need help coding a rhombus in stars
    By LinxuS in forum New To Java
    Replies: 5
    Last Post: 10-18-2008, 12:12 AM
  5. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 09:18 PM

Posting Permissions

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