在java中计算未来值时,答案是错误的

时间:2015-01-22 22:35:18

标签: java

所以我正在创建一个未来的价值应用程序,允许用户输入他们投资的金额,每年的百分比,投资的年数,以及是否每年,半年,每月,每日复合等等。

我遇到的问题是我的答案是错误的,但只有几百个。

使用我现在的代码,我使用半年的例子:

PV = 1000
Interest Rate = 3
Years Invested = 10
Compounded = Semi Annually

我得到的答案是1806.

正确答案是1346.86,据说是。

我的代码在这里:

    public void actionPerformed(ActionEvent event)
    {
        String input1 = textInitialAmount.getText();
        String input2 = textAnnualInterest.getText();
        String input3 = textNumberOfYears.getText();
        String comp = textCompounded.getText();

        double invest = Double.parseDouble(input1);
        double interest = Double.parseDouble(input2);
        double numberofyear = Double.parseDouble(input3);

        if(comp.equals("Annually"))
        {
            double compPeriod = 1;
            double compNumberOfYear = numberofyear * compPeriod;
            double rate = (interest / 100) + 1;

            double valueFuture = invest * Math.pow(rate, compNumberOfYear);
            String output = String.format("%5.0f", valueFuture);

            futureValue.setText("The future value is " + output);
        }
        if (comp.equals("Semi-Annually"))
        {
            double compPeriod = 2;
            double compNumberOfYear = compPeriod * numberofyear;
            double percentRate = interest / 100;
            double rate = percentRate + 1;

            double valueFuture = invest * Math.pow(rate, compNumberOfYear);
            String output = String.format("%5.0f", valueFuture);

            futureValue.setText("The future value is " + output);
        }
        if(comp.equals("Monthly"))
        {
            double compPeriod = 12;
            double compNumberOfYear = compPeriod * numberofyear;
            double rate = 1 + (interest / 100);

            double valueFuture = invest * Math.pow(rate, compNumberOfYear);
            String output = String.format("%5.0f", valueFuture);

            futureValue.setText("The future value is " + output);
        }
        if(comp.equals("Bi-Weekly"))
        {

        }       
    }
}

感谢您的帮助。如果您需要我解释更多,请发表评论。

1 个答案:

答案 0 :(得分:2)

您的问题与编码无关......这与您处理利率的方式有关!

  • 如果您按年利率计算利息,则每年有1个转换期。
  • 如果您每两年复合一次利息(或者您的问题所说的“半年”),那么您每年有 2 期,并且您必须将利率转换为有效的双年利率计算兴趣之前。

正确的代码是这样的:

double compPeriod = 2;
double compNumberOfYear = compPeriod * numberOfYear;
double percentRate = (interest / compPeriod) / 100;
//                    ^^^^^^^^^^^^^^^^^^^^^
//                    You missed this!
double rate = percentRate + 1;
double valueFuture = invest * Math.pow(rate, compNumberOfYear);

手工完成:

  • 利率:3%
  • 复合期:2
  • 年:10
  • 期间:2 * 10 = 20
  • Efective 利率:3%/ 2 = 1.5%
  • 投资:1000
  • 未来价值:1000 *(1 + 0.015)^ 20 = 1346.85500655

一个小礼物:更清洁,更简单的代码 (因为我是一个好人;))

你正在复制你的代码!你可以清理它:

public void actionPerformed(ActionEvent event) {
        String input1 = textInitialAmount.getText();
        String input2 = textAnnualInterest.getText();
        String input3 = textNumberOfYears.getText();
        String comp = textCompounded.getText();

        double invest = Double.parseDouble(input1);
        double interest = Double.parseDouble(input2);
        double numberofyear = Double.parseDouble(input3);
        // You just need to declare variables once!
        double compPeriod; 
        switch(comp.toLowerCase()) {
            case "annually":
                compPeriod = 1;
                break;
            case "semiannually":
                compPeriod = 2;
                break;
            case "monthly":
                compPeriod = 12;
                break;
            case "bi-weekly":
                compPeriod = 26; // Assuming 52 weeks per year
                break;
            case "weekly":
                compPeriod = 52; // Assuming 52 weeks per year
                break;
            default:
                compPeriod = 1;
        }
        double compNumberOfYear = numberofyear * compPeriod;
        double valueFuture = invest * Math.pow(1 + (interest / compPeriod) / 100, compNumberOfYear);
        String output = String.format("%5.0f", valueFuture);
        futureValue.setText("The future value is " + output);
    }
}