public class Change {
public static void main(String[] args) {
System.out.print("Enter the amount due: ");//Variable = amount
Scanner keyboard = new Scanner(System.in);
int amount = keyboard.nextInt();
System.out.print("Enter the amount recieved from Customer: ");
Scanner keyboard2 = new Scanner(System.in);
int customer = keyboard2.nextInt();//variable = customer
double change = customer - amount;
System.out.println("You need to give to customer");
double remainingAmount = (double) (change * 100);
double numDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
double numQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
double numDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
double numNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
double numPennies = remainingAmount;
System.out.println("Dollars: " + numDollars);
System.out.println("Quarters: " + numQuarters);
System.out.println("Dimes: " + numDimes);
System.out.println("Nickels: " + numNickels);
System.out.println("Pennies: " + numPennies);
}
}
When I try running the code, I only recieve back the remainder for example when i type in Amount needed as 247
and enter the customer number 315
I get 68
as the remainder in the first statement of "Dollars: "
The reason you are always only seeing a dollar amount, and no coins, is that you prompt the user to input dollar amounts. Your code then multiplies this dollar amount by 100 to determine the number of cents. Since the input is always multiples of dolars, there will never be any remainder after dividing by 100, and hence there will never be any coins.
If you want to see some coins, then allow the user to enter dollar amounts with fractional cents, as double
type input, and then you will see the cents in this (no pun intended):
System.out.print("Enter the amount due: ");//Variable = amount
Scanner keyboard = new Scanner(System.in);
double amount = keyboard.nextDouble(); // e.g. 311.00
System.out.print("Enter the amount recieved from Customer: ");
Scanner keyboard2 = new Scanner(System.in);
double customer = keyboard2.nextDouble(); // e.g. 320.49
double change = customer - amount; // 9.4900000
int remainingAmount = (int)(change * 100); // 949
int numDollars = remainingAmount / 100; // 9 (49 gets truncated)
remainingAmount = remainingAmount % 100; // 49 remainder
int numQuarters = remainingAmount / 25; // 1 (24 gets truncated)
remainingAmount = remainingAmount % 25; // 24 remainder
int numDimes = remainingAmount / 10; // 2 (4 gets truncated)
remainingAmount = remainingAmount % 10; // 4 remainder
int numNickels = remainingAmount / 5; // 0
remainingAmount = remainingAmount % 5; // 4 remainder
int numPennies = remainingAmount; // 4 pennies left
System.out.println("Dollars: " + numDollars);
System.out.println("Quarters: " + numQuarters);
System.out.println("Dimes: " + numDimes);
System.out.println("Nickels: " + numNickels);
System.out.println("Pennies: " + numPennies);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.