2016-07-07 12 views
2

私はJavaのnoobです。私は2ドルの入力を与えられた変更量を与えるプログラムを書こうとしています。私は何が起こっているのか理解できません。私は特に硬貨を出力しようとするのに困っています。例えば、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値に変換します。

正しいバージョンは、あなたは間違いなく、Javaの命名とコーディング規則上に読んでください

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."); 
      } 

    } 
    } 

を下回っています。

+0

ありがとうYogesh_D。あなたのソリューションは、私が必要としていたものでした!それは完全に動作します。私が言ったように、私はJavaとプログラミングにはまったく新しいものです。私は、命名規則とコーディング規則が間違っていることを知りたいです。 – BWMustang13

+0

私の頭の上のいくつかは、AmtPaid - 良い変数名ではない、amtPaid、別の例NumberFormat nする必要があります。また、メソッドをさまざまな小さなメソッドに分割することも賢明でしょう。おそらく、データを格納するために使用される変数の数を最適化することができます。 Googleはあなたが古い太陽のドキュメントを取得する必要がありますJavaの慣習について少し。 –

+0

もう一度Yogesh_Dに感謝します。あなたがそれを述べた後、私は今日戻って、AmtPaidとNumberFormatについてあなたが言っていたことを理解しました。ヘルプとアドバイスをありがとう。 – BWMustang13

関連する問題