Hi All,
And first of all thanks for taking the time to read my first post.
I am working through a a java textbook and am stuck on this application problem :
"Write an application that prompts for and reads a double value representing a monetary amount. Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest (assume that a ten dollar bill is the maximum size needed). For example, if the value entered is $47.63, then the program should print the equivalent amount as: 4 ten dollar bills 1 five dollar bills 2 one dollar bills 2 quarters 1 dimes 0 nickels 3 pennies " I have tackled it by using starting with a total amount (eg 5678 dollars) and using division followed by remainder operator to work down to dollars then cents. (I am working in 10dollarnotes, 5dollarnotes, 1dollarcoin, 2dollarcoin, 10centpice and 20centpiece rather than the currency in the example). I have found this to work in easier examples but I'm hopeful there is a better way..... At the moment it prints the correct value of 10dollar notes, but FAR too many 2dollarcoins and NaN for cents. Also it doesnt allow me to scan the total so I can enter and rerun with a new total for the terminal. Here is what i'm puttin in and getting out : //RevCoinValueScan.java //application which prompts for and reads a monetary amount in dollars //and cents. Then determines the fewest amount of 10dollar, 5dollar //notes, //5,10 cent 1 and 2dollar coins required to make that amount. import java.util.Scanner; public class RevCoinValueScan { public static void main (String[] args) { double note10; double dollar2; double dollar1; double cent10; double cent5; double total = 5678; double total2; double total3; double total4; double total5; Scanner scan = new Scanner (System.in); System.out.println ("Enter the monetary amount: " + total); note10 = total / 10; total2 = total % note10; dollar2 = total2 / 2; total3 = total2 % dollar2; dollar1 = total3 / 1; total4 = total3 % dollar1; cent10 = total4 / 0.10; total5 = total4 % cent10; cent5 = total5 / 5; System.out.println (" the total number of 10 dollar notes is: " + note10); System.out.println (" Two Dollar coins: " + dollar2); System.out.println (" One Dollar coins: " + dollar1); System.out.println (" Ten Cent coins: " + cent10); System.out.println (" Five Cent coins: " + cent5); } }
AND HERES WHAT I'M GETTING OUT :
1 error craig@craig-laptop:~/Documents/panda/java$ javac RevCoinValueScan.java craig@craig-laptop:~/Documents/panda/java$ java RevCoinValueScan Enter the monetary amount: 5678.0 the total number of 10 dollar notes is: 567.8 Two Dollar coins: 2.2737367544323206E-13 One Dollar coins: 0.0 Ten Cent coins: NaN Five Cent coins: NaN craig@craig-laptop:~/Documents/panda/java$ ANOTHER AMOUNT : craig@craig-laptop:~/Documents/panda/java$ javac RevCoinValueScan.java craig@craig-laptop:~/Documents/panda/java$ java RevCoinValueScan Enter the monetary amount: 786.908 the total number of 10 dollar notes is: 78.6908 Two Dollar coins: 2.8421709430404007E-14 One Dollar coins: 0.0 Ten Cent coins: NaN Five Cent coins: NaN craig@craig-laptop:~/Documents/panda/java$