我需要编写一个java程序来计算销售交易的账单和变更。
它只能分配20s,10s,5s,1s,quarter,dimes,nickles和pennies。
如果它不必分配面额(说你的变化是50美分,它应该只分配两个季度。它不应该提到另一种货币)
我无法分配正确数量的货币,它只是分配所有货币(例如,如果它是21美元而我给收银员22,代码告诉我给.013 $ 20s ,.294美元,等等。) 如果有人能帮助我,那就太好了。
double price; // declares how much the price is
double moneyGiven; // declares how much money the cashier is given
int change; // declares how much change is given in return
System.out.println("Enter the Price of the item:");
price = IO.readDouble()*100;
{
{
System.out.println("Enter the Amount Given:");
moneyGiven = IO.readDouble()*100;
{
if (moneyGiven > price)
System.out.println("Your Change is:");
change = moneyGiven - price;
int twenties = change / 2000;
if (twenties >= 1) {
change = change % 2000;
System.out.println(twenties + "$20 Bills");
}
int tens = change / 1000;
if (tens >= 1) {
change = change % 1000;
System.out.println(tens + "$10 Bills");
}
int fives = change / 500;
if (fives >= 1) {
change = change % 500;
System.out.println(fives + "$5 Bills");
}
int ones = change / 100;
if (ones >= 1) {
change = change % 100;
System.out.println(ones + "$1 Bills");
}
int quarters = change / 25;
if (quarters >= 1) {
change = change % 25;
System.out.println(quarters + "Quarters");
}
int dimes = change / 10;
if (dimes >= 1) {
change = change % 10;
System.out.println(dimes + "Dimes");
}
int nickles = change / 5;
if (nickles >= 1) {
change = change % 5;
System.out.println(nickles + "Nickles");
}
int pennies = change / 1;
if (pennies >= 1) {
change = change % 1;
System.out.println(pennies + "Pennies");
}
if (moneyGiven <= price)
System.out.println("Not enough!");
if (moneyGiven == price)
System.out.println("There is no Change to be given.");
}
}
}
}
}
答案 0 :(得分:1)
这里有些不妥之处:
if
陈述后你有分号。这将意味着总是执行大括号中的以下代码答案 1 :(得分:0)
我相信我找到了答案,但它以答案的答案回答答案,即。 1.0,5.0。如何在没有.0的情况下返回答案? 但是,我想在我的代码上输入任何内容:
double price; // declares how much the price is
double moneyGiven; // declares how much money the cashier is given
double change; // declares how much change is given in return
System.out.println("Enter the Price of the item:");
price = IO.readDouble()*100; {
{
System.out.println("Enter the Amount Given:");
moneyGiven = IO.readDouble()*100; {
if (moneyGiven > price)
System.out.println("Your Change is:");
change = moneyGiven - price;
double twenties = change / 2000;
if (twenties >= 1) {
change = change % 2000;
System.out.println(Math.floor(twenties) + "$20 Bills"); }
double tens = change / 1000;
if (tens >= 1) {
change = change % 1000;
System.out.println(Math.floor(tens) + "$10 Bills"); }
double fives = change / 500;
if (fives >= 1) {
change = change % 500;
System.out.println(Math.floor(fives) + "$5 Bills"); }
double ones = change / 100;
if (ones >= 1) {
change = change % 100;
System.out.println(Math.floor(ones) + "$1 Bills"); }
double quarters = change / 25;
if (quarters >= 1) {
change = change % 25;
System.out.println(Math.floor(quarters) + "Quarters"); }
double dimes = change / 10;
if (dimes >= 1) {
change = change % 10;
System.out.println(Math.floor(dimes) + "Dimes"); }
double nickles = change / 5;
if (nickles >= 1) {
change = change % 5;
System.out.println(Math.floor(nickles) + "Nickles"); }
double pennies = change / 1;
if (pennies >= 1) {
change = change % 1;
System.out.println(Math.floor(pennies) + "Pennies"); }
if (moneyGiven <= price)
System.out.println("Not enough!");
if (moneyGiven == price)
System.out.println("There is no Change to be given.");
}
}
}
}
}