2016-07-07 87 views
2

我对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 + " $1 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."); 
      } 


    } 
+0

使用调试器来了解发生了什么 – Jens

+0

您向我们展示了您的预期结果,但是当您执行该程序时,您的结果是什么? –

回答

1

你快到了,你有点和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 + " $1 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命名和编码约定阅读起来。

+0

谢谢Yogesh_D。您的解决方案正是我需要的!它完美的作品。就像我说的,我对Java很新,一般编程。我很想知道我所做的命名和编码惯例错误。 – BWMustang13

+0

AmtPaid - 不是一个好的变量名,应该是amtPaid,另一个例子是NumberFormat n。此外,将方法分解为各种更小的方法也是明智的。你也许可以优化用于存储数据的变量数量。谷歌有一点关于Java公约你应该得到一个旧的太阳文档。 –

+0

再次感谢Yogesh_D。在你提到它之后,我今天回去了解了你对AmtPaid和NumberFormat的看法。感谢您的帮助和建议。 – BWMustang13