这是我目前拥有的代码,它都按预期工作,但是,累计总数不起作用,我很肯定我在做一些绝对愚蠢的事情。
assume period = 20
assume inflation = 3
assume nightlycost = 100
assume nights = 7
$yearlycost = $nightlycost*$nights;
while ($period > 0) {
$period = $period-1;
$yearlyincrease = ($inflation / 100) * $yearlycost;
$nightlyincrease = ($inflation / 100) * $nightlycost;
$nightlycost = $nightlycost + $nightlyincrease;
$yearlycost = ($yearlycost + $yearlyincrease) + $yearlycost;
}
Result:
Nightly Hotel Rate in 20 years: $180.61 - <?php echo round($nightlycost, 2); ?> correct
Weekly Hotel Rate in 20 years: $1264.27 - <?php echo round($nightlycost, 2) * 7; ?> correct
Total cost to you over 20 years: $988595884.74 - <?php echo round($yearlycost, 2); ?> incorrect
除了每年的累积成本外,一切都按预期正确输出。它应该采用前一年的成本并加上该年的成本+通货膨胀。
示例:第一年是 700,所以第二年应该是 700 + 700 + 21(21 是 3%,即当年的通货膨胀率)。因此,第二年的累计总数为:1421。第三年将是 1421 + 721(去年的总数)+ 721 的 3%。
希望这足够清楚,您可以看到我哪里出错了。谢谢!