【问题标题】:JAVA money changeJAVA换钱
【发布时间】:2016-04-01 12:17:27
【问题描述】:

我正在尝试编写一个输出货币变化的代码,如下所示:

**$7.00 remains to be paid. Enter coin or note: $100.00

  You gave $100.00

Your change:

1 x $50.00

2 x $20.00

0 x $10.00

0 x $5.00

1 x $2.00

1 x $1.00

  **

但输出应该是相同的,但不显示 0 值,例如它应该是这样的:

**$7.00 remains to be paid. Enter coin or note: $100.00

  You gave $100.00

Your change:

1 x $50.00

2 x $20.00

1 x $2.00

1 x $1.00
  **

我的货币兑换代码看起来像这样,使用 if 语句:

  double fiftyDollars, twentyDollars, tenDollars, fiveDollars, twoDollars, oneDollar, fiftyCents, twentyCents, tenCents, fiveCents;

        fiftyDollars = change / 50.00; //dividing change by
        change = change % 50.00; //get remainder of the change
                if(fiftyDollars > 0) {
                    System.out.println((int) fiftyDollars + " x $50.00"); //getting output as integer- casting
                }

                twentyDollars = change / 20.00;
        change = change % 20.00;
                if (twentyDollars > 0){
                    System.out.println((int) twentyDollars + " x $20.00");
                }

        tenDollars = change / 10.00;
        change = change % 10.00;
                if (tenDollars >0) {
                    System.out.println((int) tenDollars + " x $10.00");
                }

        fiveDollars = change / 5.00;
        change = change % 5.00;
                if(fiveDollars > 0) {
                    System.out.println((int) fiveDollars + " x $5.00");
                }

        twoDollars = change / 2.00;
        change = change % 2.00;
                if (twoDollars >0) {
                    System.out.println((int) twoDollars + " x $2.00");
                }

        oneDollar = change / 1.00;
        change = change % 1.00;
                if(oneDollar >0) {
                    System.out.println((int) oneDollar + " x $1.00");
                }

        fiftyCents = change / 0.5;
        change = change % 0.5;
                if (fiftyCents >0) {
                    System.out.println((int) fiftyCents + " x $0.50");
                }

        twentyCents = change / 0.2;
        change = change % 0.2;
                if(twentyCents > 0) {
                    System.out.println((int) twentyCents + " x $0.20");
                }

        tenCents = change / 0.1;
        change = change % 0.1;
                if (tenCents > 0){
                    System.out.println((int) tenCents + " x $0.10");
                }

        fiveCents = change / 0.05;
        change = change % 0.05;
                if (fiveCents > 0){
                    System.out.println((int) fiveCents + " x $0.05");
                }

我会感谢你们的任何帮助! 谢谢你

【问题讨论】:

  • 考虑提供MCVE
  • double 替换为int 用美分计算。
  • 当您将double 转换为int 或更好的long 时,我建议您使用long cents = (long) Math.round(x * 100) 对结果进行四舍五入而不是截断它。

标签: java if-statement output


【解决方案1】:

您可以检查等于或大于 1 的值。

正如另一个答案中所说,您正在处理 double 值,因此 >0 将匹配 0.10.56 等..

所以:

if (tenDollars >= 1)

【讨论】:

    【解决方案2】:

    您使用的是双精度数,因此 0 值介于 0 和 1 之间, 如果您在 if 子句中将 double 转换为 ints,您的程序应该可以正常工作。

    【讨论】:

    • 如果您将 double 转换为 int,则数字 0.9999999 将获得 int 值 0,这不是您所期望的。最好与双倍安全边际进行比较:if(tenDollars > 0.01d)
    • 如果你的 chage 是 25 美元并且你检查 if(fiftyDollars > 0.01d)它会返回 true,因为 50/25 = 0.5,但你不会得到 50 美元的零钱
    • 是的,所以应该是:if(fiftyDollars >= 1.0d)
    【解决方案3】:

    像这样改变你所有的 if 条件

    if(fiftyDollars > 0) {
    

    if((int)fiftyDollars > 0) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2013-11-29
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多