Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

understanding this basic loop

 
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67699
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not post the same question more than once.
 
Greenhorn
Posts: 24
Python Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Grow a forest with seedballs and this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic