2016-07-07 1 views
2

Je suis noob à Java et j'essaye d'écrire un programme qui donne la quantité de changement donné deux entrées de dollar. Je ne peux pas comprendre ce qui se passe. J'ai particulièrement du mal à essayer de sortir la monnaie. Ainsi, par exemple, si une personne devait 654,32 $ et payait 987 $, son changement devrait être de 332,68 $, soit 2 trimestres, 1 pièce de dix cents, 1 pièce de nickel et 3 pièces de monnaie.Calcul de la quantité de changement due

Toute aide est grandement appréciée!

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

Utilisez un débogueur pour savoir ce qui se passe – Jens

+0

Vous nous avez montré le résultat attendu, mais quel est votre résultat maintenant lorsque vous exécutez le programme? –

Répondre

1

Vous étiez presque là, vous avez un peu mélangé avec cBal et dBal. Et la conversion du changement à leur valeur .XX.

La version correcte est inférieure à

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

    } 
    } 

Vous devriez lire définitivement sur le nommage Java et les conventions de codage.

+0

Merci Yogesh_D. Votre solution était exactement ce dont j'avais besoin! Cela fonctionne parfaitement. Comme je l'ai dit je suis très nouveau à Java, et la programmation en général. J'aimerais savoir quelles erreurs de dénomination et de codage j'ai commises. – BWMustang13

+0

Quelques choses sur le dessus de ma tête, AmtPaid - pas un bon nom de variable, devrait être amtPaid, un autre exemple NumberFormat n. Aussi, il serait sage de diviser la méthode en plusieurs méthodes plus petites. Et vous pourriez probablement optimiser le nombre de variables utilisées pour stocker des données. Google un peu sur les conventions Java, vous devriez obtenir un vieux doc soleil. –

+0

Merci encore Yogesh_D. Après l'avoir mentionné, je suis retourné aujourd'hui et j'ai réalisé ce que vous disiez pour AmtPaid et NumberFormat. Merci pour l'aide et les conseils. – BWMustang13