java 单利到期价值的求法及计算公式

mbjcgjjk  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(107)

对于到期日,我认为公式是错误的,它给出了错误的答案。我只是想每月。到期情况是在年底。任何帮助将不胜感激。

package interest;

import java.util.Scanner;

public class Interest {

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        Scanner whatKindPeriod = new Scanner(System.in);
        double principle;
        double rate;
        double period;
        double result;
        double periodNumber;
        String type;
        String matOrSimp;
        double matResult;

        System.out.println("find maturity or simple interest? for simple interest enter simple, and for maturity enter maturity");
        matOrSimp = userInput.next();
        switch (matOrSimp) {
            case "simple":
                System.out.println("please enter the principle:");
                principle = userInput.nextDouble();
                System.out.println("Enter the rate:");
                rate = userInput.nextDouble();
                System.out.println("enter period:");
                period = userInput.nextDouble();
                System.out.println("is it daily, monthly or yearly?");
                type = userInput.next();
                switch (type) {
                    case "yearly":
                        result = (principle * rate * period) / 100;
                        System.out.println("your simple interest is: $" + result);

                    case "daily":
                        double daily = period / 365;
                        result = (principle * rate * daily) / 100;
                        System.out.println("your simple interest is: $" + result);

                    case "monthly":
                        double monthly = period / 12;
                        result = (principle * rate * monthly) / 100;
                        System.out.println("your simple interest is: $" + result);

                        SimpleInterest interest = new SimpleInterest(principle, rate, period);
                }
            case "maturity":

                System.out.println("please enter the principle:");
                principle = userInput.nextDouble();
                System.out.println("Enter the rate:");
                rate = userInput.nextDouble();
                System.out.println("enter time of invesment:");
                period = userInput.nextDouble();
                double monthly = period / 12;

                matResult = (principle * (1 + rate * monthly));
                System.out.println("result is:" + matResult);

        }

    }
}
w8f9ii69

w8f9ii691#

到期日公式包括复利率,因此不应:

principle * (1 + rate * monthly)

一般情况下,您应该使用:

principle * Math.pow(1 + periodicRate, compoundingPeriods)

因此,特别是对于您的月度复利,下面的方法计算所需的到期日:

double computeMaturity(double principle, double monthlyRate, double months) {
        return principle * Math.pow(1 + monthlyRate, months);
    }

最后一点要注意的是,年利率必须以小数形式给出,而不是以百分比形式给出(10%利率= 0.1 monthlyRate)。
Complete code on GitHub

相关问题