java - 如何格式化 3 个条件的逻辑并使用前一个条件?

标签 java loops if-statement while-loop logic

不要求任何人为我编写程序

在考虑到以下条件的情况下,我无法理解如何设置程序的逻辑:

  • 每天迟到 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)

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/

相关文章:

c++ - 如何在没有函数或数组的情况下在 C++ 中创建条形图

javascript - 尝试比较数组中的项目以查看它们是否相同

java - 如何消除 switch case 或 if-else ?并执行以下操作?

java - 创建并填充二维数组

java - 从 Java RNG 的范围中删除一个数字?

java - Spark Streaming + kafka "JobGenerator"java.lang.NoSuchMethodError

java - 如何创建只接受实现 Iterable 的元素的方法

java - 从自定义数组列表创建一个新列表

python - 如何循环 JSON 数据中的值?

pandas - 如何使用条件执行 pd.fillna()