I assume this is only meant to support the standard american change (quarters, dimes, nickels and pennies). Then, it's super easy.
0) Define the values of each coin in pennies (since you're in Java, the cleanest way to do this is to define a Coin enum, and then your QUARTER can have a value of 25, etc. Java enums are really versatile since version 1.5).
1) Convert input to pennies (assuming you don't have to worry about input validation or anything, all you have to do is multiply by 100, and perhaps round to the nearest int just for posterity).
2) For each coin value, in decreasing order (start with quarters, then dimes, etc.) figure out how many coins fit in how much is left, and then: output your line, subtract the right amount, and keep moving.
I'm being a bit vague because this is your homework and you should at least have to think about it a little bit. 
I will say that if you setup your enum properly and just define a list or array of coins in the correct order, you won't need to repeat any code. Just loop over them.
|
|
Bookmarks