Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 67601
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic