【问题标题】:Round up to nickel, dime, quarters四舍五入到五角硬币、一角硬币、四分之一硬币
【发布时间】:2016-02-06 21:05:29
【问题描述】:

这是我必须做的:

编写一个程序,让少于一美元的金额找零。程序的输入必须是小于 100 的正整数,表示金额,以美分为单位。输出必须是原始货币数量以及可以构成该数量的一组硬币(硬币、硬币、镍币)。该程序必须产生包含产生给定数量所需的最小硬币数量的零钱。 不应包含任何便士。例如,输入 54 应产生如下结果:

54 美分需要 2 个季度,1 个镍

而不是

54 美分需要 2 个季度,0 个硬币,1 个镍

以下是我到目前为止所做的,但如果我输入 13、14、23、24、33、34 等,则不会四舍五入。

任何帮助将不胜感激。

import java.util.Scanner;

public class MakeChangetest {

    public static String makeChange(int change) throws BadChangeException {
        int amount;
        String x = "";
        if (change > 0 && change < 100) {
            amount = (int) ((Math.round(change / 5)) * 5);
            if (amount == 0 || amount == 100) {
                x = "No change to be given.";
            }
            if (amount == 5) {
                x = change + " cents requires " + " 1 nickel ";
            } else if (amount == 10 || amount == 20) {
                x = change + " cents requires " + (amount / 10) + " dimes ";
            } else if (amount == 15) {
                x = change + " cents requires " + (amount / 10) + " dime, "
                        + " 1 nickel ";
            } else if (amount == 25 || amount == 50 || amount == 75) {
                x = change + " cents requires " + (amount / 25) + " quaters ";
            } else if (amount == 30 || amount == 55 || amount == 80) {
                x = change + " cents requires " + (amount / 25) + " quaters, "
                        + " 1 nickel";
            } else if (amount == 35 || amount == 60 || amount == 70 || amount == 45 || amount == 85
                    || amount == 95) {
                if (amount % 25 == 10) {
                    x = change + " cents requires " + (amount / 25) + " quater, "
                            + "1 dime ";
                } else
                    x = change + "cents requires " + (amount / 25) + " quaters, "
                            + " 2 dimes ";
            } else if (amount == 40 || amount == 65 || amount == 90) {
                if (amount / 25 == 15) {
                    x = change + " cents requires " + (amount / 25) + " quater, "
                            + " 1 dime, " + " 1 nickel ";
                } else
                    x = change + " cents requires " + (amount / 25) + " quaters, "
                            + " 1 dime, " + " 1 nickel ";
            }
        } else
            throw new BadChangeException(
                    "Amount is not in the range to be given change for.");
        return x;
    }

    public static void main(String[] args) throws BadChangeException {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter an amount to calculate the amount of change to be given:");
        double t = input.nextDouble();
        try {
            System.out.println(makeChange((int) t));
        } catch (BadChangeException ex) {
            System.out.print("Amount is not in the range to be given change for.");
        }
    }
}

【问题讨论】:

    标签: math rounding


    【解决方案1】:

    当您编写 change / 5 时,change5 都是整数,因此 Java 会执行整数除法。整数除法向下取整,例如:

    (Math.round(14 / 5)) * 5 =>  (Math.round(2)) * 5 => 10
    

    要解决这个问题,您需要其中一个操作数是浮点数:

    amount = (int) ((Math.round(change / 5.0)) * 5);
    

    amount = (int) ((Math.round(change / 5.0f)) * 5);
    

    amount = (int) ((Math.round((float)change / 5)) * 5);
    

    【讨论】:

    • 不知道整数除法四舍五入,今天学到了新东西,谢谢!
    • 更正式地说,它忽略余数而不是四舍五入,这相当于同一件事......
    【解决方案2】:

    您有这么多 if 语句,请改用 switch。我将在几秒钟内用一些清理过的代码更新这个答案。

    这是一些清理过的代码,其中包含来自其他答案的修复:

    public class MakeChangetest {
    
        public static String makeChange(int change) throws BadChangeException {
            int amount;
            String x = "";
            if (0 <= change <= 100) {
                amount = (int) ((Math.round(change / 5.0)) * 5);
                switch(amount) {
                    case 0:
                    case 100:
                        x = "No change to be given.";
                        break;
                    case 5:
                        x = change + " cents requires " + " 1 nickel ";
                        break;
                    case 10:
                    case 20:
                        x = change + " cents requires " + (amount / 10) + " dimes ";
                        break;
                    case 15:
                        x = change + " cents requires " + (amount / 10) + " dime, " + " 1 nickel ";
                        break;
                    case 25:
                    case 50:
                    case 75:
                        x = change + " cents requires " + (amount / 25) + " quaters ";
                        break;
                    case 30:
                    case 55:
                    case 80:
                        x = change + " cents requires " + (amount / 25) + " quaters, " + " 1 nickel";
                        brea;
                    case 35:
                    case 60:
                    case 70:
                    case 45:
                    case 85:
                    case 95:
                        if (amount % 25 == 10) {
                            x = change + " cents requires " + (amount / 25) + " quater, " + "1 dime ";
                        } else {
                            x = change + "cents requires " + (amount / 25) + " quaters, " + " 2 dimes ";
                        }
                        break;
                    case 40:
                    case 65:
                    case 90:
                        if (amount / 25 == 15) {
                            x = change + " cents requires " + (amount / 25) + " quater, " + " 1 dime, " + " 1 nickel ";
                        } else {
                            x = change + " cents requires " + (amount / 25) + " quaters, " + " 1 dime, " + " 1 nickel ";
                        }
                }
            } else
                throw new BadChangeException(
                        "Amount is not in the range to be given change for.");
            return x;
        }
    
        public static void main(String[] args) throws BadChangeException {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter an amount to calculate the amount of change to be given:");
            double t = input.nextDouble();
            try {
                System.out.println(makeChange((int) t));
            } catch (BadChangeException ex) {
                System.out.print("Amount is not in the range to be given change for.");
            }
        }
    }
    

    因此,请始终尽量避免嵌套多个 if else 语句,否则事情会变得混乱且难以快速阅读。

    您也可以将(0 &lt; change &amp;&amp; change &lt; 100) 写为(0 &lt; change &lt; 100)。 由于(0 &lt; change &lt; 100) 不包含0100,因此代码将无法正常工作,但您实际上需要包含这些,因为您在下面的代码中检查0100

    所以应该是(0 &lt;= change &lt;= 100)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-10
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2020-04-08
      相关资源
      最近更新 更多