I have a program that creates a right triangle from a loop reading "*". I have used the code to complete the first part, but the second part calls for a manipulation to take the current pattern, flip it upside down, and then add it to the first pattern. Here is the code I have so far. Can anyone guide me through how to do the reverse loop, I was thinking another loop and a nested loop within to call the opposite pattern?

   public static void main (String[] args)
   {
      final int LIMIT = 10;

      for (int row = 1; row <= LIMIT; row++)
      {
         for (int star = 1; star <= LIMIT-row+1; star++)
            System.out.print ("*");

         System.out.println();
      }
   }
}

In your first part, you have a nested loop. I guess you do not understand what those loops are for. Before working on the second part, I would like to explain how those loops work first.

There are 2 loops -- one inside the other. If you graphically think as a 2 dimension rectangle, the loop starts working from top-left, go to the right, and the go down. So the loop works similary to that idea. The first value of row is the top row of the working rectangle area. The star is the counter of column from left to right. You start at the first row, and print out the star from left to right. However, the maximum column is being controlled by the current row number. The higher the row number, the less star (column) will be. Once it works down the row, you will see a print out as follows.

**********   row 1
*********    row 2
********     row 3
*******      row 4
******       row 5
*****        row 6
****         row 7
***          row 8
**           row 9
*            row 10

Now, you want to do your 2nd part, what you need to do is to think how you would print out the stars in another rectangle. The number of rows should still be the same because you are (vertically) flipping it. The different is how many stars you need to print out each row. If you attempt to relate the number of stars to each row, you will see it as follows.

*            row 1
**           row 2
***          row 3
****         row 4
*****        row 5
******       row 6
*******      row 7
********     row 8
*********    row 9
**********   row 10

Do you see it? The number of stars is equal to the row number. So now in the loop control, you would start counting from 1 up to the current row number you are at. Once you know that, simply copy the same code portion that display the star in the first portion, and paste it right below the first portion loops. Then update the control maximum number and you are done.

Hope this help.

commented: What an excellent explanation!! +0

So if I want to add spaces in to one of these shapes like below. It wouldnt have to do with the row number right? would there have to be another loop to set the amount of spaces as well?

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



  or like


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

Since it subtracts or adds one everytime I add a row wouldnt there be something seperate to control the spaces?

If you want to have new line between each draw, you could use System.out.println(); between these 2 loops but outside of the loops.

// draw the first triangle star
for (...) {
  ...
}

// insert new lines between 2 drawing
System.out.println();  // one new line
System.out.println();  // second new line

// draw the second triangle star
for (...) {
  ...
}

For now, you could use System.out.println() for each new line you want to insert. Later on, you will find a better solution for display multiple new line. I do not want to confuse you for now, so I propose this solution to you.

well I understand how to put in the lines outside the actual loop itself, what I am trying to do is print the stars, but with spaces before the pattern. like 9 spaces then a star, 8 spaces then a star, 7 spaces then a star as I have in the picture above. Instead of flipping it vertically I want to basically flip it horizontally. can I add a loop that prints 9 spaces and subtracts one every row line?

I see. Then you need to display space count as well. For example, the 2nd line has 9 stars. If you are going to horizontally flip it, you will need to add a space in front of those stars, correct? Then the 3rd line has 8 stars, there will be 2 spaces before, and so on. Now, notice that the total characters in each line is always 10. How many spaces in the 2nd lines (where there are 9 stars)? How many spaces in the 3rd?

**********   row 1, space 0, star 10
 *********   row 2, space 1, star 9
  ********   row 3, space 2, star 8
   *******   row 4, space 3, star 7
    ******   row 5, space 4, star 6
     *****   row 6, space 5, star 5
      ****   row 7, space 6, star 4
       ***   row 8, space 7, star 3
        **   row 9, space 8, star 2
         *   row 10, space 9, star 1

So the loop control is still the same, the only different will be how you display it. You still keep the way you display stars, but this time you are adding the display of white spaces in front of the stars using System.out.print(" "); for up to the number of white spaces should be. The System.out.print() method will display the string inside without going to a new line. The number of white spaces you need would be computed from the current display row number and the number of stars needed to be display. Please look at the diagram above.

really a nice suggestion given by Taywin.

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

in java??

DaniWeb Member Rules (which you agreed to when you signed up) include:

"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules
Post what you have done so far and someone will help you from there.

*
***
*****
***
*
in java???

commented: Read the DaniWeb rules -3
commented: or just read the previous post -3

how to print diamond in java?

1) This is an old thread. If you want to ask question, create a new one.
2) Show us your effort. This forum is not to give away code without the person learning anything.

Read his thread. If you still habe a question start your own thread.

*******

  • *

  • *

  • *
    *******

amo0lz: this is about the most ridiculous post ever.
1. this thread is years old, don't revive it
2. is your post a question, or a reply ?
3. make your post clear, what is it we are supposed to see in it ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.