2017-03-08 8 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 Ning Doubleを使用しないでください。代わりにBigDecimalを使用してください。 double/floatは、財務/計算用途には使用されません。 – Alboz

+0

[コードをフォーマットし、Java Style Guideに従って変数の名前を付ける方法を学んでください。先頭のブレースが行く場所は宗教的な戦いですが、それ以外は他にはありません。](https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md ) –

答えて

1

ループ(forまたはwhile)を使用する必要があります。このループでは、年を増やして新しい値を計算することができます。私は変数にいくつかの変更をした

注:あなたが整数ループしたかったので

  • Tの種類は、私はそれぞれendValuefinalValueEndValueFinalValueを変更int
  • です。 Javaの命名規則は、変数名の最初の文字が小さいcamelCaseです。
  • yearsTよりも優れた名前だと思いますが、それは私の個人的意見です。 Tに滞在する場合は、少なくとも小文字にする必要があります。t

次に、次のコードを使用できます。変数にendValueを保存するのは、一度だけ使用されるため、実際には必要ありません。だから、それはインライン化することができます。しかし、私はあなたの質問に近づくことに決めました。

int years = 0; 

    double endValue = investment; 

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

あなたはこのループの後、年がendValueがより大きいかfutureValueに等しいフルの年の数であることを認識する必要があります。つまり、結果として3.5年というようなことはできません。それを計算したいなら、Henryの解を使うべきです。

+1

'endValue * = 1 + rate;'を実行することでこれを単純化することができます。しかし、これはこのアプリケーションでは重要ではありません。 – Henry

+0

@SilverNak私はあなたのものに似ています。しかし、1行だけを印刷する代わりに、行全体を印刷します。あなたは私のコードをチェックして、間違っていたものを修正して最後の行を印刷することができますか? –

+0

@YusufNing 'System.out.println(...)'はループ内にあってはなりません。 'while'の'} 'の後ろに置いてください。うまくいくはずです。 – SilverNak

0

ここで間違っています... まず、あなたのIFステートメント:セミコロンで閉じたので、何もしません。また、浮動小数点の精度のために部分的に、終値は将来の見積もりと等しくなる可能性は低く、部分的には年数全体である可能性は低いためです。 だから私のような何かしようとするだろう:

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

これは私がdouble T = double(Math.log(FutureValue/investment)/ Math.log(1 + rate))をしたものです。しかしそれは仕事をしなかった –

+0

なぜあなたは式の周りに 'double(...)'を書いたのですか?それを取り除くだけです。 – Henry

関連する問題