Java复利计算

时间:2017-11-12 18:26:09

标签: java math

我的任务是计算达到5000美元所花费的年数,如果你从2500美元开始,7.5%的利息。计算应该是10年,但它输出11年。这可能是一个简单的语法错误,但我一直试图在过去的两个小时内解决这个问题而没有运气。

final double principle = 2500.00;
double accrued = 0;
final double interest = 0.075;
int year = 0;
double interest1 = 1.0 + interest;
while (accrued < 5000.00)
{
  accrued = principle * Math.pow(interest1, year);
  year++;
}
System.out.printf("It would take %d years to reach at least $5000 if you start with $2,500 with 7.5%% compound interest.", year);

1 个答案:

答案 0 :(得分:0)

试试这个:

final double principle = 2500.00;
double accrued = 0;
final double interest = 0.075;
int year = 0;
double interest1 = 1.0 + interest;
while (accrued <  5000.00)
{
     year++;
     accrued = principle * Math.pow(interest1, year);
}
System.out.printf("It would take %d years to reach at least $5000 if you start with $2,500 with 7.5%% compound interest.", year);