为什么计算不正确?它适用于一个数据集,但不适用于另一个数据集

问题描述 投票:0回答:1

我正在为我的 Java 课做一些作业。我对 Java 比较陌生,所以请原谅我可能犯的任何潜在错误。 该程序应该获取输入(花费的金额)和输出变化以及分割数字(十、个、四分之一、一毛钱、五分镍币、便士)。


import java.util.Scanner;

public class ChangeCalculator {

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.print("Enter the amount of money paid in dollars (<= $20): ");
            double amount = sc.nextDouble();
            var change = (20 - amount);
            
            //change = change * 100; //move the decimal over 2 places
            //change = (int) (change + .5); //add .5 and then chop off decimal
            //change = change / 100; //move the decimal place back
            
            //Math.ceil(change);
            
            if (change <= 20) {
                System.out.println("Total change is "+change);
                calculateAndPrintChange(change);
            } else {
                System.out.println("Invalid amount. Please enter an amount less than or equal to $20.");
            }
        }
    } //closes main

    public static void calculateAndPrintChange(double amount) {
        int amountInCents = (int) (amount * 100);
        int tens = amountInCents / 1000;
        amountInCents %= 1000;
        int fives = amountInCents / 500;
        amountInCents %= 500;
        int ones = amountInCents / 100;
        amountInCents %= 100;
        int quarters = amountInCents / 25;
        amountInCents %= 25;
        int dimes = amountInCents / 10;
        amountInCents %= 10;
        int nickels = amountInCents / 5;
        amountInCents %= 5;
        int pennies = amountInCents;
        // Print the change, handling pluralization and grammar
        //pluralization and grammar are not technically necessary, but just for aesthetics and that bonus :)
        if (tens > 0) {
            System.out.println((tens == 1) ? "1 ten, " : tens + " tens, ");
        }
        if (fives > 0) {
            System.out.println((fives == 1) ? "1 five, " : fives + " fives, ");
        }
        if (ones > 0) {
            System.out.println((ones == 1) ? "1 one, " : ones + " ones, ");
        }
        if (quarters > 0) {
            System.out.println((quarters == 1) ? "1 quarter, " : quarters + " quarters, ");
        }
        if (dimes > 0) {
            System.out.println((dimes == 1) ? "1 dime, " : dimes + " dimes, ");
        }
        if (nickels > 0) {
            System.out.println((nickels == 1) ? "1 nickel, " : nickels + " nickels, ");
        }
        if (pennies > 0) {
            System.out.println((pennies == 1) ? "1 penny" : pennies + " pennies");
        }
        // Remove the trailing comma and space if necessary
        if (System.out.toString().lastIndexOf(", ") == System.out.toString().length() - 2) {
            System.out.print(System.out.toString().substring(0, System.out.toString().length() - 2));
        }
        System.out.println();
    } //closes calculateAndPrintChange
} //closes pubClass

但是,当我输入 0.34 作为花费金额时,它输出: 总变化为 19.66 1 十, 1 五、 4个, 2个季度, 1毛钱, 1 镍, 1 便士

但是,当我输入0.76时

总变化为19.24 1 十, 1 五、 4个, 2毛钱, 3便士

应该是4便士。我已经尝试了两种方法来解决这个问题,但我仍然无法弄清楚。我已将我的方法(Math.ceil()、手动舍入)放在注释中。

java rounding currency
1个回答
0
投票

检查方法中的第一行

calculateAndPrintChange
。 在浮点计算中,像
1.29
这样的值通常实际上是
1.28999999999
。 当您乘以 100 并转换时,您将得到
128.999999
。 转换为 int 叶子
128

因此,相乘后加上 0.5 向上舍入到下一个整分。

int amountInCents = (int) (amount * 100. +.5);

© www.soinside.com 2019 - 2024. All rights reserved.