Results 1 to 6 of 6
  1. #1
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default help learning "for" loops

    well for homework we have been asked to create a for loop that creates a certain amount of $ signs when passed though the parameter of "howMany"
    and I have no idea how to tell a method how to repeat a character but im sure it has something to do with char, basically heres what I have to fill in, note this is only a method, not a whole program, no need for it to actually run.

    Java Code:
    Private void printRowOfDollarSigns (int howMany) {
    
    
    
          System.out.println();
    }
    basically can you show me how to do this?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    This prints one dollar sign:

    Java Code:
    System.out.print('$');
    ... and this prints 'howMany' dollar signs:

    Java Code:
    for (int i= 0; i < howMany; i++)
       System.out.print('$');
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    sweet, thanks man.
    just wondering because my text book is a bit vague on some bits, how does that I++ bit work, it is just adding to "i" how many $ signs to return?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by shazakala View Post
    sweet, thanks man.
    just wondering because my text book is a bit vague on some bits, how does that I++ bit work, it is just adding to "i" how many $ signs to return?
    Nope, it just adds one to variable i; you could've written this instead:"

    Java Code:
    for (int i= 0; i < howMany; i= i+1)
       ...
    Study your text book; it should mention and explain both the prefix and the postfix ++ operators.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by shazakala View Post
    sweet, thanks man.
    just wondering because my text book is a bit vague on some bits, how does that I++ bit work, it is just adding to "i" how many $ signs to return?
    ++ is the 'increment' operator so that 'i' always increments. you could also run a loop backwards with i-- (decrement). you could get powers of two with i*2. you could actually use any mathematical calculation in the third statement of a for-loop to alter the value of 'i' per run of the loop. also note that 'i' is just a variable and you could use any name instead of 'i'.

    // change every 3rd item in an array
    Java Code:
    for (int somethingNotCalledI = 3; somethingNotCalledI<array.length; somethingNotCalledI+3) {
        // somethingNotCalledI = 3, then 6, then 9... etc
        ...
    }
    Last edited by ozzyman; 04-03-2011 at 12:11 PM.

  6. #6
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. JPanel "for" loops Help
    By javaman1 in forum New To Java
    Replies: 7
    Last Post: 09-21-2010, 11:33 PM
  2. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  3. Replies: 16
    Last Post: 12-09-2008, 03:05 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Using for loops to create a "bridge" made out of hyphens
    By carlodelmundo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 12:20 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
  •