簡體   English   中英

如何使用余數找到最少數量的紙幣或硬幣 Dr Java

[英]How to use remainder to find the minimum number of bills or coins Dr Java

我正在編寫一個程序,該程序接受用戶輸入的食品訂單和支付金額,並提示發放貨幣單位的數量。 如果賬單是 30.89,稅后賬單是 31.89,那么預期結果是:

$20: 1
$10: 1
$5: 0 

.. 直到最后一分錢。

所以基本上我試圖找到改變所需的最低賬單,但我一直得到一個非常長的小數而不是整數。 我嘗試格式化它,但它不起作用。 如果需要,我是否必須更改我的變量約定(例如 int 為 double 或 double 為 int ??)? 這是我實施的:

Welcome to Sonia Shawarma!
Price List
Cheeseburger: $4.69
Falafel in a pita: $6.00

肉湯:4.50 美元

import java.util.Scanner;
public class PointOfSale
{
  public static void main(String [] args)

//declaring variables
    int burgers;
    int poutines;
    int falafels;
    double burger = 4.69;
    double poutine = 4.50;
    double falafel = 6.00;
    double item1;
    double item2;
    double item3;
    double totalbefore_tax;
    final double TAX;
    double tax_total;
    double total_final;
    double rounded_final;
    double money_paid;
    int twenties ;
    int tens;
    int fives;
    int toonies;
    int loonies;
    double quarters;
    double dimes;
    double nickels;
    double change1;
    double change2;
    double amount_bills20;
    double amount_bills10;
    double amount_bills5;
    double dollars2;
    double dollars1;
    double cents25;
    double cents10;
    double cents5;
    String date1;
    String cashier1;
    Scanner input = new Scanner(System.in);


    System.out.println("Welcome to Sonia Shawarma!");
    System.out.println("Price List");
    System.out.println("Cheeseburger: $4.69");
    System.out.println("Falafel in a pita: $6.00");
    System.out.println("Poutine: $4.50");

    //prompt user for number cheeseburgers,poutines,fries
    System.out.println("Enter the number of Cheeseburgers:");
    burgers = input.nextInt();
    System.out.println("Enter the number of Falafels:");
    falafels = input.nextInt();
    System.out.println("Enter the number of Poutines:");
    poutines = input.nextInt();

    //calculating
    item1 = (burger*burgers);
    item2 = (falafel*falafels);
    item3 = (poutine*poutines);
    totalbefore_tax = (item1+(item2)+(item3));
    TAX = 0.13;
    tax_total = totalbefore_tax*TAX;
    total_final = totalbefore_tax*1.13;
    rounded_final = 0.05*(Math.round(total_final/0.05));

    //total
    System.out.format("%-5s%-3.2f\n", "Total before tax: ",totalbefore_tax);
    System.out.format("%-5s%-3.2f\n", "Tax:", tax_total);
    System.out.format("%-5s%-3.2f\n","Final total:", rounded_final);
    System.out.format("%-5s%-3.2f\n", "Please pay:", rounded_final);

   //prompt user for twenties to nickels
    System.out.println("Enter the number of $20:");
    twenties = input.nextInt();
    System.out.println("Enter the number of $10:");
    tens = input.nextInt();
    System.out.println("Enter the number of $5:");
    fives = input.nextInt();
    System.out.println("Enter the number of Toonies($2):");
    toonies = input.nextInt();
    System.out.println("Enter the number of Loonies($1):");
    loonies = input.nextInt();
    System.out.println("Enter the number of Quarters($0.25):");
    quarters = input.nextDouble();
    System.out.println("Enter the number of Dimes($0.10):");
    dimes = input.nextDouble();
    System.out.println("Enter the number of Nickels($0.05):");
    nickels = input.nextDouble();

    //prompt money paid
    System.out.println("Customer paid:");
    money_paid = input.nextDouble();

    change1 = money_paid - rounded_final;

    System.out.format("%-5s%-3.2f\n","The change is:", change1);

    change2 = (change1*100);

    System.out.println("The minimum number of bills or coins is:");


    //calculating minumum change
    amount_bills20 = change2/2000;
    change2 = change2%2000;
    amount_bills10 = change2/1000;
    change2 = change2%1000;
    amount_bills5 = change2/500;
    change2 = change2%500;
    dollars2 = change2/200;
    change2 = change2%200;
    dollars1 = change2/100;
    change2 = change2%100;
    cents25 = change2/25;
    change2 = change2%25;
    cents10 = change2/10;
    change2 = change2%10;
    cents5  = change2/5;
    change2 = change2%5;

    //minimum number of bills needed
    System.out.println("$20:" + amount_bills20);
    System.out.println("$10:" + amount_bills10);
    System.out.println("$5:" + amount_bills5);
    System.out.println("Toonies:" + dollars2);
    System.out.println("Loonies:" + dollars1);
    System.out.println("Quarters:" + cents25);
    System.out.println("Dimes:" + cents10);
    System.out.println("Nickels:" + cents5);

    System.out.println("Printing receipt...");

我得到的輸出是歡迎來到 Sonia Shawarma!

Price List
Cheeseburger: $4.69
Falafel in a pita: $6.00
Poutine: $4.50
Enter the number of Cheeseburgers:
 [2]
Enter the number of Falafels:
 [1]
Enter the number of Poutines:
 [1]
Total before tax: 19.88
Tax: 2.58
Final total:22.45
Please pay:22.45
Enter the number of $20:
 [1]
Enter the number of $10:
 [1]
Enter the number of $5:
 [DrJava Input Box]
Enter the number of Toonies($2):
 [DrJava Input Box]
Enter the number of Loonies($1):
 [DrJava Input Box]
Enter the number of Quarters($0.25):
 [DrJava Input Box]
Enter the number of Dimes($0.10):
 [DrJava Input Box]
Enter the number of Nickels($0.05):
 [DrJava Input Box]
Customer paid:
 [30.00]
The change is:7.55
The minimum number of bills or coins is:
$20:0.3774999999999999
$10:0.7549999999999998
$5:1.5099999999999996
Toonies:1.2749999999999988
Loonies:0.5499999999999977
Quarters:2.199999999999991
Dimes:0.49999999999997724
Nickels:0.9999999999999545
Printing receipt...
Sonia Shawarma

如果您想使用雙精度,則需要使用 DecimalFormat 來限制小數字段,如下所述: 如何在 Java 中將數字四舍五入為 n 個小數位

DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println((total_bill/20));
System.out.println(df.format(total_bill/20));

輸出:1.8829999999999998 和 1.89

另一部分是你的音符數是十進制的,這當然是不能接受的。 由於它們是雙倍的,因此您需要將它們轉換為 int 以獲得整數

double total_bill = new Double(21.6643);
System.out.println("20$ notes:" +(int)total_bill/20); 
// casting to int gives the quotient integer. Output is 20$ notes:1
System.out.println("Remainder:" + df.format(total_bill%20));
//Output is: Remainder:1.664.

然后,您在所有其他紙幣和硬幣上使用此邏輯,其中剩余部分成為您正在執行或使用循環的總數。

對於分配,這可能是可以接受的,但在實際場景中,您可能需要使用 BigDecimal 而不是 double。 和往常一樣,stackoverflow 有一個很棒的帖子: Double vs. BigDecimal? .

但是 BigDecimal 不會重載普通的數學運算符,並且具有適用於所有運算的函數:

//retpre : is a BigDecimal - Total cash to return
System.out.println("20$ notes: " + retpre.divideToIntegralValue(new BigDecimal("20")).intValue());
BigDecimal tens =retpre.remainder(new BigDecimal("20"),mc);
//mc is MathContext to define precision and rounding

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM