Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以通过使用JOptionPane的输入来查找java中的复利?_Java_Joptionpane - Fatal编程技术网

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

有没有一种方法可以通过使用JOptionPane的输入来查找java中的复利?,java,joptionpane,Java,Joptionpane,我尝试了以下输入: 本金=1000,利率=4,适用时间=2(半年),过期年限=2 有人能指出错误并帮我解决吗?实际上,我只使用了Double,但问题是结果的小数点太多了。所以我不得不用你的复利公式是错误的。问题是 利率(通常定义为每年的百分比)必须通过除以100和除以一年中的时间段数转换为每个时间段的数字 就您的代码而言,我创建了一个获取输入值的方法 我还分解了复利计算,以便验证计算的每个部分的结果 这是修改后的代码 import java.text.NumberFormat; import

我尝试了以下输入:

本金=1000,利率=4,适用时间=2(半年),过期年限=2


有人能指出错误并帮我解决吗?实际上,我只使用了
Double
,但问题是结果的小数点太多了。所以我不得不用你的复利公式是错误的。问题是

利率(通常定义为每年的百分比)必须通过除以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);
    }
}

你的复利公式是错误的。问题是

利率(通常定义为每年的百分比)必须通过除以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);
    }
}
事实上,我只用了Double,但问题是 结果的小数点太多了。所以我不得不使用 大十进制

用于将结果四舍五入到所需的小数位数

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)
                        + "$");
    }
}
事实上,我只用了Double,但问题是 结果的小数点太多了。所以我不得不使用 大十进制

用于将结果四舍五入到所需的小数位数

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)
                        + "$");
    }
}

这实际上更像是一个关于复利计算的数学/金融类问题。它不属于编程范畴。因此,我把我的答案去掉了。这实际上是一个关于复利计算的数学/金融类问题。它不属于编程范畴。因此,我把我的答案删掉了。谢谢你的更正!虽然我今天早上就想出来了(实际上这是一个非常美妙的时刻),但这些安排确实更好,而且对我来说有点太混乱了,因为我刚刚开始。我将首先尝试理解它,然后实现您给出的代码中一些更高效的部分。谢谢您的更正!虽然我今天早上就想出来了(实际上这是一个非常美妙的时刻),但这些安排确实更好,而且对我来说有点太混乱了,因为我刚刚开始。我将首先尝试理解它,然后实现您给出的代码中一些更有效的部分。谢谢您的回答!这就是我想出来的-
bigdecimaci;大十进制inf;inf=BigDecimal.valueOf(数学功率(1+(r/(n*100)),t*n));CI=p.multiply(inf).设置刻度(2,舍入模式,半向上);BigDecimal P=CI.subtract(P).setScale(2,舍入模式,向上舍入一半);JOptionPane.showMessageDialog(null),“+t+”年的复利是$”+CI+,每年获得的利息是$“+P.divide(BigDecimal.valueOf(2.0))+”。在“+t+”年,它是$”+P);}谢谢回答!这就是我想出来的-
bigdecimaci;大十进制inf;inf=BigDecimal.valueOf(数学功率(1+(r/(n*100)),t*n));CI=p.multiply(inf).设置刻度(2,舍入模式,半向上);BigDecimal P=CI.subtract(P).setScale(2,舍入模式,向上舍入一半);JOptionPane.showMessageDialog(null),“+t+”年的复利是$”+CI+,每年获得的利息是$“+P.divide(BigDecimal.valueOf(2.0))+”。在“+t+”年,它是$”+P);}