gpt4 book ai didi

java - 显示变更需求信息

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:20 25 4
gpt4 key购买 nike

我需要编写一个 java 程序来计算销售交易的账单和找零。

它只能分配 20 枚、10 枚、5 枚、1 枚、25 分硬币、10 枚硬币、5 枚硬币和 1 便士硬币。

如果它不需要分配面额(假设您的零钱是 50 美分,它应该只分配两个 25 美分。它应该不提及其他货币)

我无法让它分配正确数量的货币,它只是分配所有货币(例如,如果是 21 美元,我给收银员 22,代码告诉我给 0.013 20 美元,0.294 10 美元,等等。) 如果有人能帮助我那就太好了。

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

}
}
}
}
}

最佳答案

这里有一些错误:

  1. if 语句后有分号。这意味着始终执行大括号中的以下代码
  2. 您似乎检查从 5 开始的余数是否大于 0,而不是 1。
  3. 每个面额的输出是除法的结果,可能是小数。您需要输出它的下限。

关于java - 显示变更需求信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com