programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Using Nested For Loops To Make Triangle

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
I am trying to make a program that prints a triangle made of asterisks with maximum length of user input:
So if the user inputs 5, it should look like this:
*
**
***
****
*****
****
***
**
*
I got my program to do the first loop, up until the maximum. I would appreciate some help on how to make it flip.
Is it a separate for-loop? Is it another, nested, for-loop?
Here is my code:

Bartender
Posts: 6109
6
• 1
• Number of slices to send:
Optional 'thank-you' note:

So you already know how to define the number of starts in a row in terms of x for the top half of the shape. Since you can do that, I'd think you should be able to define the number of stars in a row in terms of x for the bottom half also. Write it out on paper if it helps you to see the pattern:

and so on. We can see from the above that numStars = rowNum for the top half.

Ranch Hand
Posts: 5575
• 1
• Number of slices to send:
Optional 'thank-you' note:
As Jeff said reverse the first part, x = size - 1 and x--

Jeff Verdegan
Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:

Seetharaman Venkatasamy wrote:As Jeff said reverse the first part, x = size - 1 and x--

Funny, I don't recall saying that, or giving him the code.

Greenhorn
Posts: 17
• 1
• Number of slices to send:
Optional 'thank-you' note:
1- For such a program there must be a condition for a minimum number since we won't be able to draw a triangle if the user's input was 1. And also you should enforce the range from 1(or 2) - 50 by code so the user cannot get out of it. ( Check point 5 bellow for more details ).

2- I suggest using a JOptionPane to ask for the user's input, it would look a lot more professional .

3- For the loops you don't need anymore variables than just x, y.

4- And here, I was able to modify your code to get you what you want :

My advice would be to look at the code only, then try to re-create it yourself to get the maximum benefit and learn, otherwise you won't learn so much.

5- I made another version of the code one with if statement to ensure that the users enters a number >= 2 && <= 50. Just in case you're interested .

I hope I helped. Good Luck =)

Jeff Verdegan
Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:

Sarah Mitchell wrote:
4- And here, I was able to modify your code to get you what you want :

Please don't do that. This site is NotACodeMill.(⇐click) Doing somebody's work for him does not help him to learn. As it says on the topics page: "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

Collin Sampson
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the help. I both wrote it out and looked at the code provided. Makes sense now.

initialization = size -1
boolean expression = numStars >= 0
change = numStars-- (one less star on each line)

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
• Number of slices to send:
Optional 'thank-you' note:

Jeff Verdegan wrote:

Seetharaman Venkatasamy wrote:As Jeff said reverse the first part, x = size - 1 and x--

Funny, I don't recall saying that, or giving him the code.

Sarah Mitchell
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:

Jeff Verdegan wrote:

Sarah Mitchell wrote:
4- And here, I was able to modify your code to get you what you want :

Please don't do that. This site is NotACodeMill.(⇐click) Doing somebody's work for him does not help him to learn. As it says on the topics page: "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

Oops. I'm sorry I thought I'm helping !
Won't happen in the future, promise >.<

Collin Sampson wrote:Thanks for the help. I both wrote it out and looked at the code provided. Makes sense now.

initialization = size -1
boolean expression = numStars >= 0
change = numStars-- (one less star on each line)

Yes, but this only goes for the second half of the triangle, because if we didn't add the -1 there will be a duplication for the row in the middle, and I guess we don't want this. Right ?

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
• Number of slices to send:
Optional 'thank-you' note:

Sarah Mitchell wrote: I'm sorry I thought I'm helping !
Won't happen in the future, promise

Apologies accepted

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.