*
The moose likes Beginning Java and the fly likes Using Nested For Loops To Make Triangle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Nested For Loops To Make Triangle" Watch "Using Nested For Loops To Make Triangle" New topic
Author

Using Nested For Loops To Make Triangle

Collin Sampson
Greenhorn

Joined: Feb 27, 2012
Posts: 17
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:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

As Jeff said reverse the first part, x = size - 1 and x--
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 04, 2012
Posts: 17

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 27, 2012
Posts: 17
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

Joined: Jan 28, 2008
Posts: 5575

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.

Ahh, my bad time
Sarah Mitchell
Greenhorn

Joined: Mar 04, 2012
Posts: 17

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

Joined: Jan 28, 2008
Posts: 5575

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

Apologies accepted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Nested For Loops To Make Triangle
 
Similar Threads
Can't Break Out of a Loop
nesting loops
Need Help With For Loops
Asterick Triangles - Stuck after The first tier
Please Help