当前位置: 首页 > 知识库问答 >
问题:

复利计算器

秋和雅
2023-03-14

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

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


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

共有1个答案

戚森
2023-03-14

因此,最初的计算和发布的内容存在一些问题。compoundPayment设置在for循环之外,并且只设置了一次,因此不会发生复合。此外,还请求了术语类型,但未使用,因此每个术语都假定为年。我认为很难遵循mod的for循环逻辑(我明白了,当我们到达一个复合的日子时,我们会给出利息),但要跟踪不同的单位是很困难的(所以我去了很多年,但可以用几天和像你这样的循环来证明)。我做了简化,假设给出的利率是年利率,但你可以每天乘以365,或者每月乘以12,或者,确保你的周期和天数有相同的单位。

同样的情况是,选择双精度而不是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);
}
}
 类似资料:
  • 本文向大家介绍JavaScript中计算复利,包括了JavaScript中计算复利的使用技巧和注意事项,需要的朋友参考一下 复利公式 复利使用以下公式计算- 这里, P是本金。 R是年利率。 t是金钱被投资或借入的时间。 n是每单位t复利的次数,例如,如果每月复利一次,t以年为单位,则n的值为12。如果每季度复利一次,t以年为单位,则n的值将是4。 我们需要编写一个JavaScript函数,该函数

  • 我正在做一个程序,可以计算存款单上的基本利息。该计划要求投资金额和期限(最多五年)。取决于他们的任期是多少年,是什么决定了多少利息收入。我使用if/else语句来确定利率。然后,我使用循环打印出每年年底账户中有多少钱。我的问题是,当我运行这个程序时,钱不算在内。 这是整个代码。 这是我用10美元的投资得到的结果,只是为了简单起见,还有5年的投资。 我的主要问题是,我不知道如何让它不断地把乐趣加到总

  • 我正在制作一个方法,该方法应该计算一定时期内某个金额的利率(这些值已经在参数中定义)。这是我到目前为止拥有的代码: 我正在制作嵌套的for循环,正如您在那里看到的那样,那里缺少代码。这里我遇到了一些麻烦。第一个for循环贯穿年份,另一个应该计算总量。为了清楚变量“int年份”中定义的年份,假设它是7,那么程序应该计算每年数量的增长,因此: 主要方法如下所示: 我很感激我能得到的任何帮助!

  • 我有一个复利计算器,但当我运行代码并在它要求时输入以下数字时: 本金:10000利率:0.02年期:10 然后选择已经设置好的“年度”,这样,如果我或用户输入该特定字符串,choice变量将自动变为1(或者如果我输入单词“季度”或“月度”,则为已经设置的其他值)。然而,我应该得到的值是:$12189.94,而得到的值却是:10200.0我的代码哪里做错了?

  • 本文向大家介绍利用Javascript实现BMI计算器,包括了利用Javascript实现BMI计算器的使用技巧和注意事项,需要的朋友参考一下 前言 BMI指数(英文为Body Mass Index),是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准,当我们需要比较及分析一个人的体重对于不同高度的人所带来的健康影响时,BMI值是一个中立而可靠的指标。本文将介绍如何用JavaScript实现

  • 我正在尝试写我的第一次postgis查询 我的桌子如下所示 这是一辆id为1的车辆的gps数据。我需要计算车辆行驶的总距离,比方说2017-05-20年,以米为单位。可以有其他具有不同ID的视图。 参考(如)。https://gis.stackexchange.com/Questions/268776/Finding-Total-Distance of-Path-Along-Post-PointG