• Post Reply Bookmark Topic Watch Topic
  • New Topic

understanding this basic loop  RSS feed

 
Andrew Concilio
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
I am new to the site as well as programming with java. can anybody kindly help me explain the mechanics of the below code.





basically here are my questions
1- I understand why I need to use I for my loop as the loop will go around 20 times but I am not sure how k and J helped me create this pyramid.
2- it looks like this code only works with one character "*" because if I try to replace the star with a string, name or two or more character I will get a different shape. how can I modify my code so it will give me an exact pyramid regardless of what I type on the System.out.print
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post the same question more than once.
 
milan chovatiya
Greenhorn
Posts: 24
Java Oracle Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just try to understand for loop and trace it in paper you will get it....try it on yourself because thats the way you will get it
 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Concilio wrote:
2- it looks like this code only works with one character "*" because if I try to replace the star with a string, name or two or more character I will get a different shape. how can I modify my code so it will give me an exact pyramid regardless of what I type on the System.out.print


Take a closer look at your first loop that print a single space. This correspond to the single character that you use to draw the pyramid. If you change the length of the character to draw the pyramid then you also have to add some more spaces in that loop to make it a good pyramid.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!