所以我试图做的任务是找出一个本金达到某个值所需的年数。例如,我从 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
如何只打印最后一行?