我正在编写一个程序,用户输入食物订单和支付金额,并提示要发放的货币单位数量。如果账单是30.89,税后账单是31.89比预期的结果是:
$20: 1
$10: 1
$5: 0
直到最后一分钱等等。
所以基本上我试图找到需要改变的最小账单但是我一直得到一个非常长的小数而不是整数。我尝试格式化它,但它不会工作。我是否必须更改我的变量约定(例如int to double或double to int ??)if which which which? 这是我实施的内容:
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
答案 0 :(得分:0)
如果您想使用double,则需要使用DecimalFormat来限制小数字段,如下所述:How to round a number to n decimal places in Java
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
另一部分是你的笔记数是十进制的,哪个是不可接受的。由于它们是double,因此需要将它们转换为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