Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Nested For Loops To Make Triangle

 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jeff said reverse the first part, x = size - 1 and x--
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarah Mitchell wrote: I'm sorry I thought I'm helping !
Won't happen in the future, promise

Apologies accepted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic