我已经更新了我的代码,但是它继续将我初始化年份的数字放在运行时打印出来的表格的底部。而且我仍在尝试弄清楚如何使用打印输出部分中的 %.2d 来创建 2 位小数。因此,对此的任何反馈也会有所帮助。
import java.util.Scanner;
public class Investment {
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
double futureInvestmentValue = 0.0;
for (years = 1; years <=30; years++){
//calculate futureInvestmentValue
futureInvestmentValue += (investmentAmount * (Math.pow(1 + monthlyInterestRate, years * 12)));
System.out.print(years + "\t" + futureInvestmentValue + "\n");
}//end for
return futureInvestmentValue;
}//end futureInvestmentValue
public static void main(String[] args){
Scanner input = new Scanner (System.in);
//obtain Investment amount
System.out.print("Enter Investment amount: ");
double investmentAmount = input.nextDouble();
//obtain monthly interest rate in percentage
System.out.print("Enter annual interest rate in percentage: ");
double annualInterestRate = input.nextDouble();
//calculate annual interest rate
double monthlyInterestRate = (annualInterestRate / 1200);
int years = 1;
System.out.println("Years\t" + "Future Value");
System.out.print(years + "\t" + futureInvestmentValue(investmentAmount, monthlyInterestRate, years) + "\n");
}//end main
}//