I need to create a program that will figure out the change, along with the number of bills ranging from 20s thru 1s and coin ranging from quarters thru pennies. Outputting the highest denominations before lower denominations ..... How to improve code also, how would I account for the higher denominations before reaching lower denominations?
import java.util.Scanner;
public class ChangeReturn { public static void main(String[] args) {
Scanner scan = new Scanner(System.in); double cashPaid, purchaseAmount, temp;
int change;
int twenties, tens, fives, ones;
int quarters, dimes, nickels, pennies;
System.out.println("------------BEGIN--------------");
System.out.println("Cost of Item: $");
purchaseAmount = scan.nextDouble();
System.out.println("Amount of money given: $");
cashPaid = scan.nextDouble();
temp = (cashPaid - purchaseAmount);
System.out.println("CHANGE: $" + temp + "\n");
double coins = temp;
double bills = cashPaid;
twenties = (int)(temp/20);
bills =
tens = (int)(temp/temp);
bills =
fives = (int)(bills/2);
bills =
ones = (int)(bills/5);
bills =
quarters = (int)(temp/25);
coins %= 25;
dimes = (int)(coins/10);
coins %= 10;
nickels = (int)(coins/5);
coins %= 5;
pennies = (int)(coins/1);
coins %= 1;
System.out.println("Twenties = " + twenties);
System.out.println("Tens = " + tens);
System.out.println("Fives = " + fives);
System.out.println("Ones = " + ones);
System.out.println("Quarters = "+ quarters);
System.out.println("Dimes = " + dimes);
System.out.println("Nickels = " + nickels);
System.out.println("Pennies = " + pennies);
System.out.println("-------------END---------------");
}
}
Aucun commentaire:
Enregistrer un commentaire