有没有一种方法可以使用 JOptionPane 通过输入找到 java 的复利?

Is there a way of finding compound interest in java with inputs using JOptionPane?

我试过以下输入:

principal= 1000, rate=4, timesapplied=2(half-yearly), elapsedyears=2

import javax.swing.*;
import java.math.BigDecimal;

public class CICalc {
    public static void main(String[] args) {
        Double principal;
        Double rate;
        Double timesapplied;
        Double elapsedyears;

        principal= Double.parseDouble(JOptionPane.showInputDialog("Enter the principal amount"));
        rate=Double.parseDouble(JOptionPane.showInputDialog("Enter the Rate of Interest"));
        timesapplied=Double.parseDouble(JOptionPane.showInputDialog("Enter the number of times the principal is compounded"));
        elapsedyears=Double.parseDouble(JOptionPane.showInputDialog("Enter the amount of time(In years and if in months, write them in this format- month/12) the principal is being invested for"));

        BigDecimal CI;
        BigDecimal inf;
        inf= BigDecimal.valueOf(Math.pow(rate+(1/timesapplied*100),elapsedyears*timesapplied));

        CI= (BigDecimal.valueOf(principal)).multiply(inf);
        BigDecimal P= CI.subtract(BigDecimal.valueOf(principal));

        JOptionPane.showMessageDialog(null,"The Compound Interest for "+elapsedyears+" years is "+CI+"$ and the the interest gained is "+P+"$");

    }
}

有人可以指出错误并帮助我吗?实际上,我只使用 Double 做了这个,但问题是结果有太多的小数点。所以我不得不使用 BigDecimal.

你的复利公式是错误的。 formula

final amount = initial amount times
    1 + interest rate / number of times interest applied
    raised to the power of number of times interest applied
    times number of time periods elapsed 

通常定义为每年百分比的利率必须通过除以 100 再除以一年中的时间段数来转换为每个时间段的数字。

就你的代码而言,我做了一个获取输入值的方法。

我还分解了复利计算,以便验证每个部分的计算结果。

这是修改后的代码。

import java.text.NumberFormat;

import javax.swing.JOptionPane;

public class CICalc {
    public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getCurrencyInstance();

        double principal = getValue("Enter the principal amount");
        double rate = getValue("Enter the yearly interest rate");
        double timesapplied = getValue("Enter the number of times "
                + "the principal is compounded per year");
        double elapsedmonths = getValue("Enter the amount of time "
                + "in months the principal is invested");

        double temp1 = 1.0d + rate * 0.01d / timesapplied;
        double temp2 = timesapplied * elapsedmonths / 12d;
        double finalAmount = principal * Math.pow(temp1, temp2);

        JOptionPane.showMessageDialog(null,
                "The total amount returned after " +
                elapsedmonths +
                " months is " + nf.format(finalAmount) +
                " and the the interest gained is " +
                nf.format(finalAmount - principal));
    }

    private static double getValue(String prompt) {
        String response = JOptionPane.showInputDialog(prompt);
        return Double.parseDouble(response);
    }
}

Actually, I had made this using only Double but the problem was that the result had way too many decimal points. So I had to use BigDecimal.

使用BigDecimal setScale​(int newScale, RoundingMode roundingMode)将结果四舍五入到所需的小数位数。

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.swing.JOptionPane;

class Main {
    public static void main(String[] args) {
        double principal = Double.parseDouble(JOptionPane.showInputDialog("Enter the principal amount"));
        double rate = Double.parseDouble(JOptionPane.showInputDialog("Enter the Rate of Interest"));
        double timesapplied = Double
                .parseDouble(JOptionPane.showInputDialog("Enter the number of times the principal is compounded"));
        double elapsedyears = Double.parseDouble(JOptionPane.showInputDialog("Enter the number of years"));

        double base = 1.0 + rate / (timesapplied * 100.0);
        double exponent = elapsedyears * timesapplied;

        double inf = Math.pow(base, exponent);
        double CI = principal * inf;
        double P = CI - principal;

        JOptionPane.showMessageDialog(null,
                "The Compound Interest for " + BigDecimal.valueOf(elapsedyears).setScale(2, RoundingMode.HALF_UP)
                        + " years is " + BigDecimal.valueOf(CI).setScale(2, RoundingMode.HALF_UP)
                        + "$ and the the interest gained is " + BigDecimal.valueOf(P).setScale(2, RoundingMode.HALF_UP)
                        + "$");
    }
}