用java编写一个程序,读取用户输入的货币金额,然后确定代表该金额所需的每种纸币和硬币的最少数量,使用整除法
时间: 2023-05-24 16:04:16 浏览: 90
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入货币金额:");
double amount = scanner.nextDouble();
int dollars = (int) amount;
int cents = (int) ((amount - dollars) * 100);
int twentys = dollars / 20;
dollars = dollars % 20;
int tens = dollars / 10;
dollars = dollars % 10;
int fives = dollars / 5;
dollars = dollars % 5;
int ones = dollars;
int quarters = cents / 25;
cents = cents % 25;
int dimes = cents / 10;
cents = cents % 10;
int nickels = cents / 5;
cents = cents % 5;
int pennies = cents;
System.out.println("最少的零钱数量为:");
System.out.println("纸币:");
System.out.println("20元纸币:" + twentys);
System.out.println("10元纸币:" + tens);
System.out.println("5元纸币:" + fives);
System.out.println("1元纸币:" + ones);
System.out.println("硬币:");
System.out.println("25美分硬币:" + quarters);
System.out.println("10美分硬币:" + dimes);
System.out.println("5美分硬币:" + nickels);
System.out.println("1美分硬币:" + pennies);
}
}
阅读全文