2016-07-07 6 views
2

저는 자바에 멍청한 사람입니다. 나는 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

예상 결과를 보여 줬지만 지금 programm을 실행할 때 귀하의 결과는 무엇입니까? 감사합니다. Yogesh_D. –

답변

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

    } 
    } 

아래

올바른 버전입니다.

+0

당신의 솔루션은 내가 필요로했던 것입니다! 그것은 완벽하게 작동합니다. 내가 말했듯이, 나는 Java와 프로그래밍에 대해 아주 새로운 것이다. 나는 내가 만든 명명 규칙과 코딩 규칙을 알고 싶다. – BWMustang13

+0

AmtPaid - 좋은 변수 이름이 아니라면 AmtPaid, 또 ​​다른 예제 인 NumberFormat n을 사용해야합니다. 또한이 방법을 여러 가지 작은 방법으로 나누는 것이 현명 할 것입니다. 그리고 아마도 데이터 저장에 사용되는 변수의 수를 최적화 할 수 있습니다. Google은 자바 관습에 대해 당신은 오래된 태양 의사를 받아야한다. –

+0

다시 감사합니다 Yogesh_D. 당신이 그것을 언급 한 후에 나는 오늘 되돌아 가서 AmtPaid와 NumberFormat에 대해 당신이 말하는 것을 깨달았습니다. 도움과 조언에 감사드립니다. – BWMustang13