I just started coding in java and I am working on a change machine-esk program. I know It can be condensed but Its part of the constraints. It keeps out putting random change owed and quarter count...
import java.util.Scanner;
public class Change {
public static void main(String[] args) {
double costVar, paidVar, amountOwed;//user defined
//defining the vale of US coins
final double quarterCoin = 0.25;
final double dimeCoin = 0.10;
final double nickelCoin = 0.05;
final double pennyCoin = 0.01;
//Variable for math stuff
double quarterAmountdec, dimeAmountdec, nickelAmountdec, pennyAmountdec;
short quarterAmountfin, dimeAmountfin, nickelAmountfin, pennyAmountfin;
Scanner keyboard = new Scanner(System.in);
//ask the user to input costs and amount payed (assuming the amount paid is > or = the cost of the item)
System.out.println("Enter Item Cost: ");
costVar=keyboard.nextDouble();
System.out.println("Enter Amount Paid: ");
paidVar=keyboard.nextDouble();
amountOwed = paidVar - costVar;//math for the changed owed
System.out.println("\nChange Owed: $" +amountOwed++);//displaying how much change the machine owes
//math to calculate the coins owed (involves intentional loss of accuracy
quarterAmountdec = amountOwed / quarterCoin;
quarterAmountfin = (short)quarterAmountdec;
//outputs coins owed
System.out.println("Quarters: " +quarterAmountfin++ );
}
}
Output:
Enter Item Cost:
2.34
Enter Amount Paid:
6.89Change Owed: $4.55
Quarters: 22