ANSWERS: 1
  • I'm not familiar with Java, but I can give you an algorith based on C++ which I am familiar with. The key is to use the % operator to fidn the remainder of a division problem. Basically, each IF statement checks to see if the amount of money entered is high enough to MAKE a roll out of it, starting with the largest (quarters), and working it's way down. It first determines the number of rolls that can be made by dividing by the whole dollar amount of the roll (10 dollars for a roll of quarters, 5 for nickels...and so on, and assignes that number to the integer variable. The next line of the IF statement actually DOES the operation that removes that amount of money from "amount". 10.36 for example after running through the second line of the first IF statement would become .36. ------------------------------------------------------- float amount int quarters, dimes, nickels, pennies If (amount >= 10) quarters = amount / 10 amount = amount % 10 If (amount >= 5) dimes = amount / 5 amount = amount % 5 if (amount >= 2) nickels = amount / 2 amount = amount % 2 if (amount >= .5) pennies = amount /.5 amount = amount % .5 ------------------------------------------------------- You will be left with the number of rolls in each integer variable, and "amount" will be the amount left over that cannot be made into rolls. Also keep into mind that this programs takes advantage of the fact that under C, if you assign a float to an integer, it "chops off" everything after the decimal point to give you the whole number. This is an advantage here, but if Java does not do this (or does not allow for such an assignment) you will have to adjust for this. Good luck!

Copyright 2023, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy