【问题标题】:In Java, how to apply interest on certain amount of the total amount在Java中,如何对总金额的一定金额应用利息
【发布时间】:2015-03-06 04:45:54
【问题描述】:

我有一个问题需要用 Java 解决。基本上,我必须计算上个月到今天为止客户花了多少度电。从 0 到 300 千瓦时,总计 5 美元。从 301 到 1000 千瓦时,前 300 千瓦时 5 美元,然后每千瓦时额外 0.03 美元。从 1001 起,每千瓦时额外增加 0.02 美元。我不确定如何在 300kwh 之后在每个 kwh 上应用这些 0.03 和 0.02。谢谢。

【问题讨论】:

  • 没有电脑你会怎么做?使用铅笔和纸,写下您将使用的步骤...
  • 家庭作业?如果是这样,这是陌生人之一。
  • 这是一个家庭作业。我也在纸上尝试过,我到了某个点,但是当我想在 Java 中使用 IF 语句时,当我必须在 300kwh 之后应用这种兴趣时,我遇到了困难。
  • 没有循环,只有 IF 语句,没有开关。
  • 如果是家庭作业,您仍然应该展示您所做的研究/努力以及您遇到的问题并将其写在您的问题中。这不是一个为我编写代码的网站;问题应该不仅对发帖人有用,而且对阅读您的问题的其他人也有用。

标签: java math if-statement rate


【解决方案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.

从那个伪代码开始。以下是一些基于您的 cmets 的实际代码:

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;

【讨论】:

  • 有趣。例如
  • If(totalReading>300) totalReading=oldReading*0.03 ?
  • 1000-totalReadingtotalReading > 1000 时为负数。所以最好加上 * 0.01 - 但这与减去 totalReading-1000 相同。
  • 对。我将再次键入程序。感谢您的帮助埃里克。不幸的是,今晚无法完成作业,但明天早上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2023-04-07
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多