0

我已经更新了我的代码,但是它继续将我初始化年份的数字放在运行时打印出来的表格的底部。而且我仍在尝试弄清楚如何使用打印输出部分中的 %.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
}//
4

2 回答 2

0

您永远不会修改用于计算此值的任何术语annualInterest

double annualInterest = (1-1 / Math.pow(investmentAmount * (1 + monthlyInterestRate), years * 12));

于 2013-02-23T21:34:40.023 回答
0

您返回的值与从 Scanner 获得的值相同:

双投资金额 = input.nextDouble();

您应该返回 futureInvestmentValue。

而且,正如其他评论中所指出的,您正在循环,但您并没有改变,而是改变了循环中变量的值,因此您可以避免循环或检查逻辑。

于 2013-02-23T21:36:24.803 回答