【问题标题】:Calculating investment duration in Java using simple interest formula使用单利公式计算 Java 中的投资期限
【发布时间】:2017-03-08 10:40:52
【问题描述】:

所以我试图做的任务是找出一个本金达到某个值所需的年数。例如,我从 5000 美元开始,我想以 10% 的利率/年累积 15000 美元。我想知道该投资的持续时间有多长

这是我目前所做的

package com.company;

import java.util.Scanner;


public class InvestmentDuration {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println ("Initial Investment: ");
    double investment = input.nextDouble();

    System.out.println ("Rate as decimals: ");
    double rate = input.nextDouble();

    System.out.println ("Future Value: ");
    double FutureValue = input.nextDouble();

    double T = 0; //Initialise Duration//

    double EndValue = investment * Math.pow ((1+rate), T); //Formula of Simple Interest//

     while (EndValue < FutureValue) {
        T += 1.0;

         if (investment * Math.pow((1 + rate), T) == FutureValue);

        System.out.println("The Number of years it takes to accumulate $" + FutureValue + " is " + T + " years");
    }


}

输出:

The Number of years it takes to accumulate $15000.0 is 1.0 years
The Number of years it takes to accumulate $15000.0 is 2.0 years
The Number of years it takes to accumulate $15000.0 is 3.0 years
The Number of years it takes to accumulate $15000.0 is 4.0 years
The Number of years it takes to accumulate $15000.0 is 5.0 years
The Number of years it takes to accumulate $15000.0 is 6.0 years
The Number of years it takes to accumulate $15000.0 is 7.0 years
The Number of years it takes to accumulate $15000.0 is 8.0 years
The Number of years it takes to accumulate $15000.0 is 9.0 years
The Number of years it takes to accumulate $15000.0 is 10.0 years
The Number of years it takes to accumulate $15000.0 is 11.0 years
The Number of years it takes to accumulate $15000.0 is 12.0 years

如何只打印最后一行?

【问题讨论】:

标签: java loops finance rate computational-finance


【解决方案1】:

最简单的解决方案是使用一些数学知识:

Math.log(goal/start) / Math.log(1+rate/100.0)

其中goalstart 分别是末尾和开头的金额,rate 是利率百分比。

【讨论】:

  • 这就是我所做的 double T = double (Math.log(FutureValue/investment) / Math.log (1 + rate));但它没有工作
  • 你为什么在公式周围写double( ... )?只需删除它。
【解决方案2】:

您需要使用循环(forwhile)。在这个循环中,您可以增加年份并计算新值。

请注意,我对变量做了一些更改:

  • 因为你想要一个整数循环,T 的类型是int
  • 我将EndValueFinalValue 分别更改为endValuefinalValue。 Java 命名约定为驼峰式,变量名的首字母较小。
  • 我认为yearsT 更好,但这是我个人的看法。如果你决定留在T,至少应该是小写字母t

然后你可以使用下面的代码。将 endValue 保存在变量中并不是真正必要的,因为它只使用一次。所以它可以被内联。但我决定继续回答您的问题。

    int years = 0;

    double endValue = investment;

    while (endValue < futureValue) {
        years++;
        endValue = investment * Math.pow((1 + rate), years);
    }

您应该知道,在这个循环之后,years 是endValue 大于或等于futureValue 的整年数。这意味着你不能有像 3.5 年这样的结果。如果你想计算,你应该使用亨利的解决方案。

【讨论】:

  • 可以通过endValue *= 1 + rate; 来简化这一点。但这在这个应用程序中并不重要。
  • @SilverNak 我做了和你类似的事情。但不是只打印一行,而是打印整行。你能检查我的代码我做错了什么以及要修复什么,所以它只打印最后一行吗?
  • @YusufNing System.out.println(...) 不应该在循环中。将它放在while 的关闭} 之后,它应该可以工作。
【解决方案3】:

这里有很多错误... 首先,您的 IF 语句:它没有任何动作,因为您已用分号关闭它。此外,endvalue 不太可能等于 futurevalue,部分原因是浮点精度,部分原因是不太可能是整数年。 所以我会尝试类似的东西:

double T = 0.0;
while(investment * Math.pow ((1+rate), T) < FutureValue) {
   T += 1.0;
}
System.out.println ("The Number of years it takes to accumulate the Future Value is: " +T+);

当然,您也可以重新调整公式以直接计算 T。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多