编写程序,读取用户输入的代表总金额的double值,打印表示该金额所需的最少纸币张数和硬币个数,打印从最大金额开始。纸币的种类有十元、五元、一元,硬币的种类有五角、一角、贰分、壹分。用java编写
时间: 2023-03-30 15:03:43 浏览: 125
您好,我可以回答这个问题。以下是Java代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入总金额:");
double totalAmount = scanner.nextDouble();
int[] bills = {10, 5, 1};
int[] coins = {50, 10, 2, 1};
String[] billNames = {"十元", "五元", "一元"};
String[] coinNames = {"五角", "一角", "贰分", "壹分"};
System.out.println("需要的纸币和硬币如下:");
for (int i = ; i < bills.length; i++) {
int count = (int) (totalAmount / bills[i]);
if (count > ) {
System.out.println(billNames[i] + ":" + count + "张");
totalAmount -= count * bills[i];
}
}
for (int i = ; i < coins.length; i++) {
int count = (int) (totalAmount / coins[i]);
if (count > ) {
System.out.println(coinNames[i] + ":" + count + "个");
totalAmount -= count * coins[i] / 100.;
}
}
}
}
希望能帮到您。
阅读全文