[英]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
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.