我以为我有这个正确的,但是当我尝试在我的“收银机”中拆分付款时,它在第二次付款和第一次付款时都返回了正确的更改,但是在第二次付款时,它包括我放置的第三个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.00Here 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.
更多推荐
发布评论