计算应得的零钱数额

Calculating the amount of change due

我是 Java 的菜鸟,我正在尝试编写一个程序,在输入两美元的情况下给出找零的金额。我无法弄清楚发生了什么。我在尝试输出造币时遇到了特别的麻烦。因此,例如,如果一个人欠 654.32 美元并支付了 987 美元,他们的零钱应该是 332.68 美元,即 2 夸特、1 角钱、1 镍币和 3 便士。

非常感谢任何帮助!

import java.util.*;
import java.math.*;
import java.text.*;


public class CorrectChange {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        BigDecimal AmtDue;      
        BigDecimal AmtPaid;


        NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);

        //User enters the amount that the customer owes
        System.out.println("Enter the amount below that is due to be paid.");
        System.out.print("$");
            AmtDue = input.nextBigDecimal();

            //Converts user's input into the currency format
            AmtDue = AmtDue.setScale(2, BigDecimal.ROUND_HALF_UP);
                double dAmtDue = AmtDue.doubleValue();
                    String eAmtDue = n.format(dAmtDue);

        //User enters the amount that the customer paid     
        System.out.println("Enter the amount below that has been paid.");
        System.out.print("$");
            AmtPaid = input.nextBigDecimal();

            //Converts user's input into the currency format
            AmtPaid = AmtPaid.setScale(2, BigDecimal.ROUND_HALF_UP);
                double dAmtPaid = AmtPaid.doubleValue();
                    String eAmtPaid = n.format(dAmtPaid);

            //Checks to see if the amount paid is more than the amount owed
            if (AmtDue.compareTo(AmtPaid)> 0){
                double dBal = AmtDue.subtract(AmtPaid).doubleValue();
                String eBal = n.format(dBal);
                System.out.println("You still owe: " + eBal.toString());
            }

            //Checks to see if the amount owed is more than the amount paid
            if (AmtDue.compareTo(AmtPaid)< 0){
                int cBal = (AmtPaid.compareTo(AmtDue)*100);
                double dBal = AmtPaid.subtract(AmtDue).doubleValue();
                String eBal = n.format(dBal);


                    int DolBills = (int) (dBal / 1);
                    dBal = dBal % 1;

                    int quarters = (int) (dBal / 25);
                    dBal = dBal % 25;

                    int dimes = (int) (cBal / 10);
                    cBal = cBal % 10;

                    int nickles = (int) (cBal / 5);
                    cBal = cBal % 5;

                    //pennies = (int) (dBal / 100);
                    //dBal = dBal % 100;

                System.out.println("You owe a balance of " + eAmtDue.toString() + ". Since you paid the amount of " + eAmtPaid.toString() + " your change is " + eBal.toString());
                System.out.println("Your change amount is as follows:\n" +
                    "\t" + DolBills + "  dollar bills \n" +
                    "\t" + quarters + " quarters \n" +
                    "\t" + dimes + " dimes \n" +
                    "\t" + nickles + " nickels \n");
                    "\t" + numPennies + " pennies";

            }

            //Checks to see if the amount paid is equal to the amount owed
            if (AmtDue.compareTo(AmtPaid)== 0){
                    System.out.println("Your balance has been paid. Thank you for your business.");
            }


    }

你快到了,你有点混淆了 cBal 和 dBal。并转换为它们的.XX值。

正确的版本如下

import java.util.*;
import java.math.*;
import java.text.*;


public class CorrectChange {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        BigDecimal AmtDue;      
        BigDecimal AmtPaid;


        NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);

        //User enters the amount that the customer owes
        System.out.println("Enter the amount below that is due to be paid.");
        System.out.print("$");
            AmtDue = input.nextBigDecimal();

            //Converts user's input into the currency format
            AmtDue = AmtDue.setScale(2, BigDecimal.ROUND_HALF_UP);
                double dAmtDue = AmtDue.doubleValue();
                    String eAmtDue = n.format(dAmtDue);

        //User enters the amount that the customer paid     
        System.out.println("Enter the amount below that has been paid.");
        System.out.print("$");
            AmtPaid = input.nextBigDecimal();

            //Converts user's input into the currency format
            AmtPaid = AmtPaid.setScale(2, BigDecimal.ROUND_HALF_UP);
                double dAmtPaid = AmtPaid.doubleValue();
                    String eAmtPaid = n.format(dAmtPaid);

            //Checks to see if the amount paid is more than the amount owed
            if (AmtDue.compareTo(AmtPaid)> 0){
                double dBal = AmtDue.subtract(AmtPaid).doubleValue();
                String eBal = n.format(dBal);
                System.out.println("You still owe: " + eBal.toString());
            }

            //Checks to see if the amount owed is more than the amount paid
            if (AmtDue.compareTo(AmtPaid)< 0){
                double dBal = AmtPaid.subtract(AmtDue).doubleValue();
                String eBal = n.format(dBal);


                    int DolBills = (int) (dBal / 1);
                    dBal = dBal % 1.0;

                    int quarters = (int) (dBal / .25);
                    dBal = dBal % .25;

                    int dimes = (int) (dBal / .10);
                    dBal = dBal % .10;

                    int nickles = (int) (dBal / .05);
                    dBal = dBal % .05;

                    int numPennies = (int) (dBal / .01);
                    //dBal = dBal % 0.01;

                System.out.println("You owe a balance of " + eAmtDue.toString() + ". Since you paid the amount of " + eAmtPaid.toString() + " your change is " + eBal.toString());
                System.out.println("Your change amount is as follows:\n" +
                    "\t" + DolBills + "  dollar bills \n" +
                    "\t" + quarters + " quarters \n" +
                    "\t" + dimes + " dimes \n" +
                    "\t" + nickles + " nickels \n" +
                    "\t" + numPennies + " pennies");

            }

            //Checks to see if the amount paid is equal to the amount owed
            if (AmtDue.compareTo(AmtPaid)== 0){
                    System.out.println("Your balance has been paid. Thank you for your business.");
            }

    }
    }

您一定要仔细阅读 java 命名和编码约定。