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

    Default Tips for java exercise: print steps of n height

    Hi,

    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
    Posts
    2,571
    Rep Power
    9

    Default

    Uhm, with two nested loops?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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
    Posts
    32
    Rep Power
    0

    Default

    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-14-2010, 12:14 AM
  2. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-14-2010, 12:13 AM
  3. Need Java career tips
    By computerbum in forum Jobs Discussion
    Replies: 2
    Last Post: 05-05-2010, 03:36 PM
  4. Java Newbie Overall Tips
    By Hossein in forum New To Java
    Replies: 4
    Last Post: 04-05-2010, 11:32 PM
  5. Replies: 4
    Last Post: 06-25-2008, 09: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
  •