これは、プリンシパルが特定の値に達するまでに要した年数を調べることです。たとえば、私は$ 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
どのように私はちょうど最後の行を印刷しますか?
@Yusuf Ning Doubleを使用しないでください。代わりにBigDecimalを使用してください。 double/floatは、財務/計算用途には使用されません。 – Alboz
[コードをフォーマットし、Java Style Guideに従って変数の名前を付ける方法を学んでください。先頭のブレースが行く場所は宗教的な戦いですが、それ以外は他にはありません。](https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md ) –