我在Java中有一个问题需要解决。基本上,我必须计算KWH客户上个月到目前为止已经花了多少钱。从0-300 KWH,总计5美元。从301到1000kwh,前300kwh为5美元,然后每kwh额外增加0.03美元。从1001及以上每千瓦时额外收费0.02美元。我不确定如何在300kwh后每千瓦时应用0.03和0.02。感谢。
答案 0 :(得分:1)
从5美元开始。这是金额。
If the total is greater than 300 kwh,
then the amount should be increased by $0.03 per kwh above 300.
If the total is greater than 1000 kwh,
then the amount should be reduced by $0.01 per kwh above 1000.
从那个伪代码开始。以下是基于您的评论的一些实际代码:
int totalReading = ...; // something
// $5 is the minimum cost
double cost = 5.0;
// above 300, the cost is 0.03 per
if (totalReading > 300)
cost += (totalReading - 300) * 0.03;
// above 1000, the cost drops 0.01 per
if (totalReading > 1000)
cost -= (totalReading - 1000) * 0.01;