【问题标题】:Compounding interest calculator复利计算器
【发布时间】:2019-12-08 01:59:33
【问题描述】:

我的目标是创建一个程序,询问用户金额,询问每年、每月或每天的利率,询问其复利方式,然后询问以月、日或年为单位的期限.

然后它会打印未来价值以及获得的总利息。 这是我到目前为止所拥有的,数字不正确。 如果有人可以帮助修改并使其工作,我将非常感激。


import java.util.Scanner;

public class Compunding {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        double compoundingTerms;
        double period = 0;

        System.out.println("Enter an amount of money: ");
        double amount = sc.nextDouble();
        System.out.println("Enter an rate of Interest: ");
        double rate = sc.nextDouble();
        System.out.println("Enter per years, months, or days: ");
        String time = sc.next();
        System.out.println("Enter how it will be componded monthly, semi-anually, quarterlly, anually: ");
        String compoundRate = sc.next();
        System.out.println("Enter the term amount: ");
        double term = sc.nextDouble();
        System.out.println("Enter the term type (Monthy,Yearly,Daily}: ");
        String termType = sc.next();

        if (time.equals("years")) {
            period = 1;
        }
        if (time.equals("months")) {
            period = 12;
        }
        if (time.equals("days")) {
            period = 365;
        }

        if (compoundRate.equals("monthly")) {
            rate = (rate / 100) / 12;
            term = term * 12;
        }
        if (compoundRate.equals("semi-anually")) {
            rate = (rate / 100) / 2;
            term = term * 2;
        }
        if (compoundRate.equals("quarterlly")) {
            rate = (rate / 100) / 4;
            term = term * 4;
        }
        if (compoundRate.equals("anually")) {
            rate = rate / 100;
            term = term * 1;
        }

        double compoundPayment = 0;

        for (int i = 1; i <= term; i++ ) {
            if (i % period == 0 ) {
                colInterest(amount, rate);
            }
            compoundPayment = amount * (1.0 + rate);
        }

        System.out.println("The Final payment will be: " + compoundPayment);
    }

    public static double colInterest(double valueAmount, double valueInterest) {
        return valueAmount * valueInterest;
    }
}

【问题讨论】:

  • colInterest(金额,利率);被调用,但它的值没有分配给任何东西。为什么?
  • compoundPayment = 金额 * (1.0 + 费率);在 for 循环外调用,并将报告的金额设置为仅收集一次利息的数字。在我看来,您的意思是在循环中设置复合付款和/或随时更新金额。
  • 谢谢,我继续解决了这个问题。但是,问题仍然存在,我从程序中获得的数字不正确。您将如何编写程序以从我给出的程序描述中输出正确的数字?

标签: java math


【解决方案1】:

因此,原始计算和发布的内容存在许多问题。 CompoundPayment 设置在 for 循环之外,并且仅设置一次,因此不会发生复合。此外,请求了术语类型但未使用,因此每个术语都假定为年。我认为用 mod 来遵循 for 循环的逻辑也很困难(我明白了,当我们遇到事情复杂的一天时,我们会产生兴趣),但是跟踪各种单位是很棘手的(所以我去了很多年,但一个人可以做一个几天的案例和一个像你这样的循环)。我确实简化并假设给出的费率是每年,但您可以每天计算并乘以 365,或者每月计算并乘以 12,或者,只需确保您的周期和天数具有相同的单位。

同样情况下,选择 Double 而不是 BigDecimal 来代表金钱是我跟随您并回答所提问题的一种情况。我并不是在争论我在这里回答的是最好的方法(并且可以通过使用货币而不是假设它是美元来增强)。

另一种方法是使用指数来处理重复乘法,或者,即使不是,也可以简化 for 循环(它允许您在此过程中执行诸如打印语句之类的操作并允许对货币进行四舍五入)。

我不会修复潜在的增强功能,比如一年中并不总是有 365 天,或者很好地格式化小数或更积极地检查输入。我试图给出一种可能的方式。

一个微妙之处是 numPeriods 的转换为 (int),假设其他部分有效(我测试了每年复利 364 天没有利息,但 365 天有利息),确保不要对期间给予部分利息未完成。

希望对你有帮助。

import java.util.Scanner;

public class Compounding {
private Scanner sc;

Compounding() {
    sc = new Scanner(System.in);
}


public double getAmount() {
    //enhancement: catch number format exceptions, negative numbers, etcetera, and presumbaly use a loop to retry
    System.out.println("Enter an amount of money: ");
    return sc.nextDouble();
}

//return interest as a rate
public double getInterestRate() {
    //enhancement, validate input, catch errors
    System.out.println("Enter an annual percent rate of interest: ");
    double rate = sc.nextDouble();
    return rate / 100;
}

public int getTimesCompoundedPerYear() {
    System.out.println("Enter how it will be componded monthly, semi-anually, quarterly, anually: ");
    String compoundRate = sc.next();
    if (compoundRate.equals("monthly")) {
        return 12;
    } else if (compoundRate.equals("semi-anually")) {
        return 2;
    } else if (compoundRate.equals("quarterly")) {
        return 4;
    } else if (compoundRate.equals("annually")) {
        return 1;
    } else {
        System.out.println("Unrecognized compounding, defaulting to monthly");
        return 12;
    }
}



//return term amount, units still tbd
//allowing for decimals in case someone says 6.5 years for dsomey=thing compounded more than once a year
public double getTermAmount() {
    //enhancement, validate input, catch errors
    System.out.println("Enter term amount: ");
    return sc.nextDouble();
}

public String getTermUnits() {
    System.out.println("Enter the term type (years, months, days): ");
    String termType = sc.next();
    if (termType.equals("years") || termType.equals("months") || termType.equals("days")) {
        return termType;
    } else {
        System.out.println("Unrecognized time period, defaulting to years.");
        return "years";
    }
}


public static void main(String[] args) {
    Compounding compounding = new Compounding();
    double period = 12;
    double amount = compounding.getAmount();
    double annualRate = compounding.getInterestRate(); //interest rates are always quoted as annual, no need to vary that
    int timesCompoundedPerYear = compounding.getTimesCompoundedPerYear();
    double term = compounding.getTermAmount();
    String termUnits = compounding.getTermUnits();
    double ratePerPeriod = annualRate / timesCompoundedPerYear;
    double timeInYears = term;
    if (termUnits.equals("months")) {
        timeInYears /= 12;
    } else if (termUnits.equals("days")) {
        timeInYears /= 365;
    }
    int numPeriods = (int) timeInYears * timesCompoundedPerYear;



    double compoundPayment = amount * Math.pow(1 + ratePerPeriod, numPeriods);

    System.out.println("The Final payment will be: " + compoundPayment);
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多