每月复利兴趣Java应用程序

时间:2013-09-23 20:06:40

标签: java

我必须制作一个计算每月复利的应用程序。本金为1000美元,税率为2.65%。我试图编写应用程序代码并在某些方面取得了成功。但是,我在实际数学方面遇到了问题,并尝试了不同的方法来获得复合兴趣并没有成功。链接粘贴在下面。任何帮助将不胜感激,谢谢。

http://pastebin.com/iVaWHiAJ

import java.util.Scanner;

class calculator{

private double mni, mni2, mni3;
private double intot = 1000;
private int a, c;

        double cinterest (int x){
                for(a=0;a<x+1;a++){
                        mni = intot * .0265;
                        intot = mni + intot;
                        //mni3 = (intot - mni) - 1000;
                        mni3 = (intot - mni);

                }
                return(mni3);
        }
}

class intcalc{

        public static void main(String[] args){

                calculator interest = new calculator();
                Scanner uinput = new Scanner(System.in);
                int months[] = {2, 5, 10, 500};
                int b;

                        for(b=0;b<4;b++){
                                System.out.println("Interest at " +
                                months[b] + " months is: " + interest.cinterest(months[b]));

                        }

        }

}

2 个答案:

答案 0 :(得分:3)

比这更简单。首先,您可以使用Math.pow而不是执行循环来进行复合。在这种情况下最简单的方法就是使用静态方法:

public class CalcInterest{

  public static double getInterest(double rate, int time, double principal){
    double multiplier = Math.pow(1.0 + rate/100.0, time) - 1.0;
    return multiplier * principal;
  }

  public static void main(String[] args){
    int months[] = {2, 5, 10, 500};
    for(int mon : months)
        System.out.println("Interest at " + mon + " months is " + getInterest(2.65,mon,1000));

  }

}

这是输出:

Interest at 2 months is 53.70224999999995
Interest at 5 months is 139.71107509392144
Interest at 10 months is 298.94133469174244
Interest at 500 months is 4.7805288652022874E8

答案 1 :(得分:1)

您应该对复利率背后的数学进行更多阅读。 Here是计算复利的简单指南。阅读并理解后,您应该使cintrest代码看起来像 -

double cintrest(int x){
    return intot - (intot(1+.0265)^x);
}

我在这里使用你的命名约定,但你真的需要做一些更好的名字。

相关问题