• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loop problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem: Observe that its base and height are both equal to, and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size.

The output of the program should be like this for n=4.
          #
       ##
   ###
####

My issue in this problem:  The output(for n=4) is
#
##
###
####

can you please help me out? what I'm doing wrong in my code. Thanks. The code is given below.





 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you design that program? What pseudo‑code did you use? Those staircase programs can be annoying, but proper design in advance will beat them into submission.
 
Ranch Hand
Posts: 47
1
IntelliJ IDE Slackware Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is uh, this what you're hoping to achieve?

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use a StringBulder myself, to improve performance over multiple System.out.println calls. I wouldn't call toString() on it, nor would I do any removal. Nor would I use the insert() method. I would add all the lines to my StringBuilder, so only one println call would be necessary. I personally would use a separate method to create the lines.
But the real problem OP is facing is working out the loops. I would like to know how he calculated the number of spaces and of hash signs, since that is not clear from the code. The code looks like what happens when you simply shove bits of code together without planning, though I do find that sort of staircase program awkward to write myself. That is why it is a worthwhile exercise: it makes people think about how many times each loop will run.
The worst error is in line 37.

OP: Please tell the full details. You described an output, but I got a different output from that.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell, How about using characters instead of Strings ? That would probably boost the program even more. I see that just 3 characters: ' ','#', and '\n' are needed.
@OP, the only import you need is I am not sure why you are using scanner.skip, maybe you can elaborate more on that ?
 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi @Kabi, you can do something like this:

You need to print n lines, all with the same length:
n-1 blanks + 1 #
n-2 blanks + 2 #
.
.
.
n-(n-1) blanks + n-1 #
0 blanks + n #

hope this helps
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, two of you already started providing complete solutions, that is of no good for OP.
That way we are simply robbing from OP an ability to come up with his/her own piece of solution he/she could proud of.

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.

 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Slackware Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aye, I apologize.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies accepted
reply
    Bookmark Topic Watch Topic
  • New Topic