• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me write this method!  RSS feed

 
king java
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how to even approach this.. beginner here, any help would be appreciated I can't seem to figure out how to get even spacing to form a pyramid

Write a public static method named starPrinter that will take an int as a parameter and
print lines of stars as shown below.

The header of the method will be public static void starPrinter(int n) .

e.g.



Thank you so much!
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its simple..

You have to start from 1,

keep on adding 2 in every loop run until you reach n..

for number of spaces you have to leave (int)(n/2) spaces before and after your print.. and you have to decrease this number every new line by 2..
 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"king java", please check your private messages regarding an important administrative matter.
 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please ShowSomeEffort by showing us what you have done up to now?
You may also find some interesting instructions in HowToAskQuestionsOnJavaRanch
 
Niels Tielenburg
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using for loops, first without the spaces like this:

*
***
*****

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vicky chauhan wrote: . . . spaces before and after your print . . .
No, you only need spaces before printing; nobody will ever know whether you have spaces after the ***
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!