【问题标题】:why is java turning integers into something less [duplicate]为什么java将整数变成更少的东西[重复]
【发布时间】:2014-01-26 03:48:04
【问题描述】:

为什么 java 会把一个原本是 10 的数字变成像 9.999999999999998 这样的数字? 这是我的代码。此外,任何关于良好做法的建议将不胜感激。例如,我可以做什么来代替所有 if 语句。我的代码的目的是输入一些东西的成本和支付了多少。输出将是差额以及由此产生的必须退回的美元和硬币。这是我遇到困难的代码。

package another;

import javax.swing.JOptionPane;

/**
 *
 * @author Will
 */
public class Another {

    public static void tryAgain() {
        JOptionPane.showMessageDialog(null, "Enter a valid amount and try again");
        System.exit(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String purchase = JOptionPane.showInputDialog("Please enter a the purchase amount");

        if (purchase == null) {
            System.exit(0);
        }

        if (purchase.isEmpty()) {
            tryAgain();
        }

        for (int i = 0; i < purchase.length(); i = i + 1) {
            if (!Character.isDigit(purchase.charAt(i))
                    && !purchase.contains(".")) {
                tryAgain();
            }
        }

        String given = JOptionPane.showInputDialog("Please enter the given amount");

        if (given == null) {
            System.exit(0);
        }

        if (given.isEmpty()) {
            tryAgain();
        }

        for (int i = 0; i < given.length(); i = i + 1) {
            if (!Character.isDigit(given.charAt(i))
                    && !given.contains(".")) {
                tryAgain();
            }
        }

        Double a = Double.parseDouble(purchase);
        Double b = Double.parseDouble(given);
        Double c = b - a;

        if (c < 0) {
            JOptionPane.showMessageDialog(null, "Give the cashier more money!");
            System.exit(0);
        }

        System.out.println( "Change: $" + c);

        if (c > 100) {
            double hundredsPlusExtra = c / 100;
            double hundredsWithout = Math.floor(hundredsPlusExtra); //this line rounds down
            c = c - hundredsWithout * 100;
            if (hundredsWithout == 1) {
                System.out.println(hundredsWithout + " One-Hundred Dollar Bill");
            } else {
                System.out.println(hundredsWithout + " Hundred Dollar Bills");
            }
        }

        if (c > 50) {
            double fiftiesPlusExtra = c / 50;
            double fiftiesWithout = Math.floor(fiftiesPlusExtra); //this line rounds down
            c = c - fiftiesWithout * 50;
            if (fiftiesWithout == 1) {
                System.out.println(fiftiesWithout + " Fifty Dollar Bill");
            } else {
                System.out.println(fiftiesWithout + " Fifty Dollar Bills");
            }
        }

        if (c > 20) {
            double twentiesPlusExtra = c / 20;
            double twentiesWithout = Math.floor(twentiesPlusExtra); //this line rounds down
            c = c - twentiesWithout * 20;
            if (twentiesWithout == 1) {
                System.out.println(twentiesWithout + " Twenty Dollar Bill");
            } else {
                System.out.println(twentiesWithout + " Twenty Dollar Bills");
            }
        }

        if (c > 10) {
            double tensPlusExtra = c / 10;
            double tensWithout = Math.floor(tensPlusExtra); //this line rounds down
            c = c - tensWithout * 10;
            if (tensWithout == 1) {
                System.out.println(tensWithout + " Ten Dollar Bill");
            } else {
                System.out.println(tensWithout + " Ten Dollar Bills");
            }

        }

        if (c > 5) {
            double fivesPlusExtra = c / 5;
            double fivesWithout = Math.floor(fivesPlusExtra); //this line rounds down
            c = c - fivesWithout * 5;
            if (fivesWithout == 1) {
                System.out.println(fivesWithout + " Five Dollar Bill");
            } else {
                System.out.println(fivesWithout + " Five Dollar Bills");
            }

        }

        if (c > 1) {
            double onesPlusExtra = c / 1;
            double onesWithout = Math.floor(onesPlusExtra); //this line rounds down
            c = c - onesWithout * 1;
            if (onesWithout == 1) {
                System.out.println(onesWithout + " One Dollar Bill");
            } else {
                System.out.println(onesWithout + " One Dollar Bills");
            }
        }

        if (c > .25) {
            double quartersPlusExtra = c / .25;
            double quartersWithout = Math.floor(quartersPlusExtra); //this line rounds down
            c = c - quartersWithout * .25;
            if (quartersWithout == 1) {
                System.out.println(quartersWithout + " Quarter");
            } else {
                System.out.println(quartersWithout + " Quarters");
            }
        }
    }
}

【问题讨论】:

  • 如何用二进制表示十进制数?
  • 我在您的代码中看到的唯一整数是 for 循环索引和整数文字。
  • 对于投反对票的人,请在投反对票之前尝试搜索有关此主题的答案。 (不幸的是)根据问题中的关键字找到这个问题的相关答案并不容易。
  • 我同意@AndrewThompson 的观点。每当我在关闭之前找到一个问题时,我都会将这些问题标记为重复,但我不会投反对票。对于那些已经意识到浮点舍入误差是一种普遍现象的人来说,与前面问题的关系是显而易见的。对于不了解舍入误差的人来说,它们看起来像是独特而奇怪的情况。

标签: java swing integer


【解决方案1】:

【讨论】:

    【解决方案2】:

    我承认,我没有阅读您的代码,但我 99% 确定您遇到了二进制数据存储问题。

    你看,在十进制系统中,1/10 可以很容易地表示为 0.1。但是,尝试 1/3,它变成 0.33333333333333333...

    在二进制中,1/10 是 0.0001100110011001100110011...

    查看 BigDecimal 以获取 base10 数字存储。

    【讨论】:

    • 在这个和许多其他简单的情况下,在每个输出完成工作之前正确舍入。
    猜你喜欢
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2015-06-12
    • 2019-08-17
    • 2021-03-26
    • 2019-05-26
    相关资源
    最近更新 更多