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

How do I format the logic for 3 conditions and use a previous condition as well?

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

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

(Example: a book returned 10 days late incurs a late fee of .30 late fee, [=11=].10 for each of the first 7 days plus [=11=].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?

这是我最好的尝试,但我不确定这是最有效的逻辑:

    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 > 90)
    {
        costDue = bookPrice + 10;
    }
    else if (daysLate > 7 && daysLate <= 90)
    {
        costDue = (7*0.10) * (daysLate*0.20);
    }
    else
    {
        costDue = daysLate*0.10;
    }

只需更新第二个子句并删除最后一个子句,即可解决问题。

第二个条件要改,否则第三个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();
}
if (daysLate > 90)
{
    costDue = bookPrice + 10;
}
else if (daysLate >= 0)
{
    costDue = daysLate * 0.10;
    if (daysLate > 7) {
        costDue += (daysLate - 7) * 0.10;
    }
} else {
    IO.reportBadInput();
}

对所有先前答案的加法 - 中期(7 天到 90 天之间)的计算应该加法而不是乘法 (7*0.10) + (daysLate*0.20); 甚至 1.30 + (daysLate*0.20);(如问题描述所示)

当你使用 if 语句时,你必须确保它将你想要分开的情况分开。在您的代码中,第一个 else if 语句包含 daysLate <= 90 的所有实例,因此第二个 else if 永远不会执行。

还要检查您过去 7 天的到期计算,您似乎会在前 7 天向人们多收钱。

另一种方法,同时牺牲一点可读性,并消除一个 else-if 条件:

    if (daysLate > 90)  {
        costDue = bookPrice + 10;
    }
    else if (daysLate >= 0 && daysLate <= 90) {
        costDue = Math.min(7, daysLate) * 0.10 + Math.max(daysLate - 7,  0) * 0.20;
    } 
    else {
        IO.reportBadInput();
    }