不要求任何人为我编写程序
在考虑到以下条件的情况下,我无法理解如何设置程序的逻辑:
- 每天迟到 7 天(含 7 天),滞纳金为 0.10 美元/天
- 7 天后,每天迟到 90 天(含 90 天),滞纳金为 0.20 美元/天。
(Example: a book returned 10 days late incurs a late fee of $1.30 late fee, $0.10 for each of the first 7 days plus $0.20 for each of the remaining 3 days)
block 引用>This is where I am having issues. I am not sure how to include the previous condition along with this one. My best guess is some sort of nested loop or an elseif containing this condition and something about the last one?
- 90 天后,未归还的图书将被视为丢失。所有滞纳金均被免除,但借阅者需支付图书费用,外加 10 美元的补货费。
这是我最好的尝试,但我不确定这是最有效的逻辑:
if (daysLate > 90) { costDue = bookPrice + 10; } else if (daysLate <= 90) { costDue = (7*0.10) * (daysLate*0.20); } else if (daysLate <= 7) { costDue = daysLate*0.10; } else { IO.reportBadInput(); }
最佳答案
应更改第二个条件,否则将无法达到第三个 if
。另外,应更改第三个条件以检查 daysLate
变量是否大于或等于零:
if (daysLate > 90)
{
costDue = bookPrice + 10;
}
else if (daysLate >= 7)
{
costDue = (7*0.10) + ((daysLate - 7) * 0.20); // change here
}
else if (daysLate >= 0)
{
costDue = daysLate*0.10;
}
else
{
IO.reportBadInput();
}
关于java - 如何格式化 3 个条件的逻辑并使用前一个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873049/