我的目标是创建一个程序,向用户询问金额,询问每年,每月或每天的利率,询问如何复利,然后询问数月,数天或数年的期限。

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


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

最佳答案

因此,原始计算和发布的内容存在许多问题。在for循环的外部设置了compoundPayment,并且只设置了一次,因此不会发生复合。同样,术语类型是必需的,但没有使用,因此每个术语都假定为年。我认为使用mod遵循for循环的逻辑也是很困难的(我明白了,当我们遇到复杂的一天时,我们会产生兴趣),但是要跟踪各个单元((所以我花了很多年,但可能要花几天时间,才能像你这样循环。我确实做了简化,并假设给出的费率是每年,但是您可以将其每天设置为365,或者每月设置为12,或者确保您的期间和天数具有相同的单位。

同样是这样,我选择跟随Double而不是BigDecimal代表钱是您领导并回答所问问题的一种选择。我并不是在说我要回答的是最好的方法(可以通过使用Currency而不是假设以美元表示的货币来增强这种方法)。

一种不同的方法是使用指数来处理重复的乘法,或者即使不是,也可以简化for循环(这使您可以沿途执行诸如print语句之类的操作并允许对货币进行四舍五入)。

我不是在修正潜在的增强功能,例如一年中并不总是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);
}
}

关于java - 复利计算器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59231837/

10-11 12:17