2017-03-08 33 views
1

因此,我试图做的任务是找出委托人达到某个特定值所需的年数。比如说我从5000美元开始,我想用10%的利率/年累计15000美元。我想找到多久这是我迄今所做的投资使用简单利息公式计算Java中的投资持续时间

的持续时间

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 

我怎么只打印最后一行?

+0

@Yusuf宁不要使用double,请使用BigDecimal!双/浮动不用于财务/计算用途。 – Alboz

+0

[根据Java风格指南,学习如何格式化代码并命名变量。主要大括号去的地方是宗教战斗,除此之外,其他所有东西都是现成的。](https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md ) –

回答

1

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

注意,我做了一些改动变量:

  • 既然你想一个整数循环的T类型是int
  • 我分别改变EndValueFinalValueendValuefinalValue。 Java命名约定是camelCase,变量名首字母小。
  • 我认为years是一个比T更好的名字,但这是我个人的意见。如果你决定留在T,至少它应该是一个小写字母t

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

int years = 0; 

    double endValue = investment; 

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

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

+1

可以通过执行'endValue * = 1 + rate'来简化这一点。但这在这个应用中并不重要。 – Henry

+0

@SilverNak我已经做了类似于你的。但不是只打印一行,而是打印整行。你能检查我的代码,我做错了什么和修复,所以它只是打印最后一行? –

+0

@优先'System.out.println(...)'不应该在循环中。将它放在'while'的关闭'}'后面,它应该可以工作。 – SilverNak

0

很多错误在这里... 首先,你的IF语句:它没有动作,因为你已经用分号关闭它。而且,endvalue不太可能等于futurevalu,部分原因是浮点数的准确性,部分原因是不可能是整年数。 所以我会尝试这样的:

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,。

3

的最简单的解决方案是与位数学:

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

其中goalstart是在端部,并在分别和rate起始量在百分之利率。

+0

这就是我做的双倍T = double(Math.log(FutureValue/investment)/ Math.log(1+ rate));但它不起作用 –

+0

为什么你在公式的周围写上“double(...)”?只要删除它。 – Henry