Results 1 to 6 of 6
Thread: help learning "for" loops
- 04-03-2011, 10:25 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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.
basically can you show me how to do this?Java Code:Private void printRowOfDollarSigns (int howMany) { System.out.println(); }
- 04-03-2011, 10:30 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,370
- Blog Entries
- 7
- Rep Power
- 17
This prints one dollar sign:
... and this prints 'howMany' dollar signs:Java Code:System.out.print('$');
kind regards,Java Code:for (int i= 0; i < howMany; i++) System.out.print('$');
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-03-2011, 10:39 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
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?
- 04-03-2011, 10:57 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,370
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
-
++ 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 11:11 AM.
- 04-03-2011, 12:17 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
JPanel "for" loops Help
By javaman1 in forum New To JavaReplies: 7Last Post: 09-21-2010, 10:33 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
hey im new to java, and i need some help with "while loops"
By neilmx7 in forum New To JavaReplies: 16Last Post: 12-09-2008, 02:05 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Using for loops to create a "bridge" made out of hyphens
By carlodelmundo in forum New To JavaReplies: 7Last Post: 09-21-2008, 11:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks