Java 复利计算器

Java 复利计算器,java,math,Java,Math,我的目标是创建一个程序,要求用户提供金额,询问每年、每月或每天的利率,询问如何进行复利,然后询问月、日或年的期限 然后,它将打印未来价值以及获得的总利息。 这是我到目前为止所拥有的,而且数字不正确。 如果有人能帮我修改并使之生效,我将不胜感激 import java.util.Scanner; public class Compunding { public static void main(String[] args) { Scanner sc = new Sc

我的目标是创建一个程序,要求用户提供金额,询问每年、每月或每天的利率,询问如何进行复利,然后询问月、日或年的期限

然后,它将打印未来价值以及获得的总利息。 这是我到目前为止所拥有的,而且数字不正确。 如果有人能帮我修改并使之生效,我将不胜感激


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


导入java.util.Scanner;
公共类计算{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
双重复合词;
双周期=0;
System.out.println(“输入金额:”);
双倍金额=sc.nextDouble();
System.out.println(“输入利率:”);
加倍率=sc.下一次加倍();
System.out.println(“输入年、月或日:”);
字符串时间=sc.next();
System.out.println(“输入每月、半年、季度、每年的组合方式:”);
字符串compoundRate=sc.next();
System.out.println(“输入术语金额:”);
双项=sc.nextDouble();
System.out.println(“输入术语类型(Monthy、year、Daily}:”);
字符串termType=sc.next();
如果(时间等于(“年”)){
周期=1;
}
如果(时间等于(“月”)){
周期=12;
}
如果(时间等于(“天”)){
周期=365;
}
如果(复合比率等于(“每月”)){
比率=(比率/100)/12;
期限=期限*12;
}
if(复合等于(“半年度”)){
比率=(比率/100)/2;
术语=术语*2;
}
if(复合等于(“四分之一”)){
比率=(比率/100)/4;
术语=术语*4;
}
如果(复合等于(“每年”)){
比率=比率/100;
术语=术语*1;
}
双重复合支付=0;

对于(int i=1;i因此,原始计算和发布的内容存在许多问题。compoundPayment设置在for循环之外,并且只有一次,因此没有发生复合。此外,请求了术语类型,但没有使用,因此每个术语都假定为年。我认为也很难遵循for循环w的逻辑对于mod(我明白了,当我们到达一个复合日时,我们会给出利息),但是跟踪不同的单位是很困难的(所以我去了很多年,但是你可以用几天和像你一样的循环来证明).我确实简化了,并假设给出的利率是年利率,但你可以每天乘以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);
}
}

调用了colInterest(金额、费率);但未将其值分配给任何对象。为什么?compoundPayment=金额*(1.0+费率);在for循环外调用,并将报告的金额设置为一个只收取一次利息的数字。在我看来,您的意思是在循环内设置复合付款和/或在执行时更新金额。谢谢,我继续并修复了该问题。但是,问题仍然存在,我从该程序获得的数字不正确。如何你能写程序输出我给你的程序描述中正确的数字吗?