四舍五入到镍、角钱、四分之一

Round up to nickel, dime, quarters

这是我必须做的:

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

54 美分需要 2 个 25 美分,1 个镍币

而不是

54 美分需要 2 个 25 美分,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.");
        }
    }
}

当您写 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);

你有这么多 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 < change && change < 100)写成(0 < change < 100)。 并且代码无法正常工作,因为 (0 < change < 100) 不包括 0100 但实际上你需要包括那些,因为你正在检查 0100 在下面的代码中。

所以它应该是 (0 <= change <= 100)