如何正确显示75美分,30美分硬币和1美分硬币的数量?

时间:2018-03-12 17:26:02

标签: java

我有一项任务,我必须创建一种方法,我必须从客户那里收钱,然后根据价格给他们换回。所需的金额为$130$55$25$5$1$0.75$0.30和{ {1}}。

目前令人沮丧的情况是它无法正确显示硬币数量,但成功的是美元数量。例如,如果某个项目的费用为$0.01而有人提供了$0.94,那么更改应为每个金额的1个(1x $218,1x $130,1x {{1} },1x $55等等。)。但每当我要求这些值时,我会得到两个$25,零$5$1以及六个$0.75硬币。

$0.30

1 个答案:

答案 0 :(得分:0)

我把你的程序搞砸了一点调试,但它的功能就像一个魅力:

    public class change {

    static int ot, numot, ff, numff, tf, numtf, f, numf, o, numo, scc, nuscc, tcc, utcc, occ;
    static double numscc, numtcc, numocc;

    public static void main(String[] args) {
        double change = 218 - 0.94;

        if (change >= 0.01 && change / 130 >= 1) {
            numot = (int) change / 130;
            change %= 130;
            numff = (int) change / 55;
            change %= 55;
            numtf = (int) change / 25;
            change %= 25;
            numf = (int) change / 5;
            change %= 5;
            numo = (int) change / 1;
            change %= 1;
            numscc = change / 0.75;
            change %= 0.75;
            numtcc = change / 0.30;
            change %= 0.30;
            numocc = change / 0.01;
        } else {
            System.err.println("Other value");
        }

        System.out.println(numot + " $130 bills");
        System.out.println(numff + " $55 bills");
        System.out.println(numtf + " $25 bills");
        System.out.println(numf + " $5 bills");
        System.out.println(numo + " $1 bills");
        System.out.println((int) numscc + " $0.75 coin");
        System.out.println((int) numtcc + " $0.30 coin");
        System.out.println((int) numocc + " $0.01 coin");
    }

}

因此,如果您遇到问题,我建议这是因为某种IO错误,因为您的逻辑不正确。