gpt4 book ai didi

java - 收银机返回剩余金额加上二次付款的零钱

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

我以为我的做法是正确的,但是当我尝试在“收银机”中拆分付款时,它在第二次和第一次付款上都返回了正确的找零,但在第二次付款上,它包含我放置的第三个 if 语句,并且它也给出了找零。

例如:

Price: $100.00
Sales tax = $6.00
Total amount due = $106.00
Payment = $50.00
Amount Remaining: $56.00
Payment $50.00
Amount Remaining: $6.00
Your Change Is: $44.00

这是有问题的代码段。如有任何帮助,我们将不胜感激。

 public void makePayment (double payment){


if (payment < 0){
System.out.println("Insufficient Funds. Please Use Another Method Of Payment");
}

if (payment < currentAmountDue ){
currentAmountDue = currentAmountDue - payment;
System.out.println("Amount Remaining: " + fmt.format(currentAmountDue));
dailySales = currentAmountDue + dailySales;
}

if (payment > currentAmountDue){
dailySales = dailySales + currentAmountDue;
currentAmountDue = payment - currentAmountDue;
System.out.println("Your Change Is: " + fmt.format(currentAmountDue));
numberOfCustomers = numberOfCustomers + 1;
dailySales = currentAmountDue + dailySales;
}

if (payment == currentAmountDue){
dailySales = dailySales + currentAmountDue;
numberOfCustomers = numberOfCustomers + 1;
currentAmountDue = 0;
System.out.println("Thank you for your money!");
}
}

最佳答案

如果您查看第二次付款的代码,很容易发现这一点。

输入后currentAmountDue=56 payment=50,所以

if (payment < currentAmountDue )

这是真的,你也这么做

 currentAmountDue = currentAmountDue - payment;

现在 currentAmountDue=6 并且仍然 payment=50 并且执行下一个 if 语句:

if (payment > currentAmountDue){

其计算结果也为true

您只想执行一个 if 分支,因此最好将除第一个之外的所有 if 分支更改为 else if

关于java - 收银机返回剩余金额加上二次付款的零钱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28557509/

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