首页 > 解决方案 > 如何从单利中获得到期价值及其方程

问题描述

对于成熟度,我认为公式是错误的,它给出了错误的答案。我只想每月一次。到期案例在最后。任何帮助,将不胜感激。

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);

        }

    }
}

标签: javamathfinance

解决方案


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

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)。

GitHub上的完整代码

希望这可以帮助。


推荐阅读