用java 完成整钱兑零钱程序,要求:输入一个double值,代表总零钱数,输出清单,列出等价的货
答案:1 悬赏:60 手机版
解决时间 2021-04-14 19:35
- 提问者网友:寂寞撕碎了回忆
- 2021-04-14 13:27
用java 完成整钱兑零钱程序,要求:输入一个double值,代表总零钱数,输出清单,列出等价的货
最佳答案
- 五星知识达人网友:西风乍起
- 2021-04-14 14:12
public static void splitMoney(Double unSplitMoney) {
if (unSplitMoney <= 0) {
System.out.println("请输入正确的金额.");
}
int unSplitMoneyCents = (int) (unSplitMoney * 100);
int[] splitArray = {100 * 100, 50 * 100, 20 * 100, 10 * 100, 5 * 100, 2 * 100, 1 * 100, 5 * 10, 2 * 10, 1 * 10, 5, 2, 1};
String[] splitArrayName = {"100元", "50元", "20元", "10元", "5元", "2元", "1元", "5角", "2角", "1角", "5分", "2分", "1分"};
System.out.println("您的输入金额为:" + unSplitMoney + "元,可以被兑换为:");
for (int i = 0; i < splitArray.length && unSplitMoneyCents > 0; i++) {
if (unSplitMoneyCents >= splitArray[i]) {
System.out.print(unSplitMoneyCents / splitArray[i] + "个" + splitArrayName[i]+",");
unSplitMoneyCents = unSplitMoneyCents % splitArray[i];
}
}
}
测试用例:
public static void main(String[] args) {
splitMoney(128.32);
}
输出结果:
您的输入金额为:128.32元,可以被兑换为:
1个100元,1个20元,1个5元,1个2元,1个1元,1个2角,1个1角,1个2分,
Process finished with exit code 0
if (unSplitMoney <= 0) {
System.out.println("请输入正确的金额.");
}
int unSplitMoneyCents = (int) (unSplitMoney * 100);
int[] splitArray = {100 * 100, 50 * 100, 20 * 100, 10 * 100, 5 * 100, 2 * 100, 1 * 100, 5 * 10, 2 * 10, 1 * 10, 5, 2, 1};
String[] splitArrayName = {"100元", "50元", "20元", "10元", "5元", "2元", "1元", "5角", "2角", "1角", "5分", "2分", "1分"};
System.out.println("您的输入金额为:" + unSplitMoney + "元,可以被兑换为:");
for (int i = 0; i < splitArray.length && unSplitMoneyCents > 0; i++) {
if (unSplitMoneyCents >= splitArray[i]) {
System.out.print(unSplitMoneyCents / splitArray[i] + "个" + splitArrayName[i]+",");
unSplitMoneyCents = unSplitMoneyCents % splitArray[i];
}
}
}
测试用例:
public static void main(String[] args) {
splitMoney(128.32);
}
输出结果:
您的输入金额为:128.32元,可以被兑换为:
1个100元,1个20元,1个5元,1个2元,1个1元,1个2角,1个1角,1个2分,
Process finished with exit code 0
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯