Results 1 to 4 of 4
  1. #1
    maniku is offline Member
    Join Date
    May 2011
    Rep Power

    Default Tips for java exercise: print steps of n height


    Java beginner here, stuck with an exercise in a programming course I'm taking.

    The exercise is: write a program that asks the user for height of steps, i.e. number of lines, and then prints the steps like so:

    E.g. height = 3

    I.e. the number of line corresponds to the number of stars on the line.

    I know the star symbol needs to be handled as a string. I know how to write the code to ask the user for number of lines. I know how to write a loop to print n number of lines with a string or an int on each, and one to print n number of stars on one line, but I'm stuck on how to combine the two.

    Could someone give me a pointer on where to go with this? Thank you greatly in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Uhm, with two nested loops?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    Look at using the print() command. It will print on the same line as the last print().
    That will allow you to control how many *s are printed on a line.

    Another way would be to have a LONG string of ****s and use substring to extract the number you want to print on the current line.

  4. #4
    javajames is offline Member
    Join Date
    May 2011
    Rep Power


    System.out.println() prints a (new) line, as norm pointed out, System.out.print() just prints, similair to printf or std::cout in c++ (or print() sys.stdout.softspace=false print() in python)

Similar Threads

  1. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-13-2010, 11:14 PM
  2. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-13-2010, 11:13 PM
  3. Need Java career tips
    By computerbum in forum Jobs Discussion
    Replies: 2
    Last Post: 05-05-2010, 02:36 PM
  4. Java Newbie Overall Tips
    By Hossein in forum New To Java
    Replies: 4
    Last Post: 04-05-2010, 10:32 PM
  5. Replies: 4
    Last Post: 06-25-2008, 08:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts