收银机退还余额加上次要付款的变更(Cash Register Returns Amount Remaining Plus Change on Secondary Payment)

系统教程 行业动态 更新时间:2024-06-14 17:00:14
收银机退还余额加上次要付款的变更(Cash Register Returns Amount Remaining Plus Change on Secondary Payment)

我以为我有这个正确的,但是当我尝试在我的“收银机”中拆分付款时,它在第二次付款和第一次付款时都返回了正确的更改,但是在第二次付款时,它包括我放置的第三个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!"); } }

I thought I had this correct, but when I tried splitting the payments in my "Cash Register", it returned the correct change on both the second and first payments, but on the second payment it includes the third if statement I placed, and it gives the change as well.

For example:

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

Here is the offending code segment in question. Any help is appreciated.

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并且currentAmountDue=56 payment=50 ,那么

if (payment < currentAmountDue )

是的,你做到了

currentAmountDue = currentAmountDue - payment;

所以现在currentAmountDue=6并且仍然payment=50 ,你执行下一个if -statment:

if (payment > currentAmountDue){

这也评估为true 。

你只想执行一个if -branches,所以你最好把所有的if改成除了第一个else if 。

It's quite easy to spot if you walk through your code for the second payment.

Upon entry currentAmountDue=56 and payment=50, so

if (payment < currentAmountDue )

is true and you do

currentAmountDue = currentAmountDue - payment;

So now currentAmountDue=6 and still payment=50 and you execute the next if-statment:

if (payment > currentAmountDue){

Which also evaluates as true.

You would only want to execute one of your if-branches, so you best change all of your if except the first to else if.

更多推荐

本文发布于:2023-04-18 00:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/23d716c401e10cbfa26dd024b0b81e5c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收银机   余额   Register   Cash   Returns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!