我的目标是创建一个程序,向用户询问金额,询问每年,每月或每天的利率,询问如何复利,然后询问月份,几天或几年内的期限
然后将打印出未来价值以及获得的总利息。 到目前为止,这是我所得到的,并且数字不正确。 如果有人可以帮助修改它并使它起作用,我将非常感激。
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;
}
}
答案 0 :(得分:1)
因此,原始计算和发布的内容存在许多问题。 compoundPayment在for循环外部设置,并且仅设置一次,因此不会发生复合。同样,术语类型是必需的,但没有使用,因此每个术语都假定为年。我认为使用mod遵循for循环的逻辑也是很困难的(我明白了,当我们遇到复杂的一天时,我们会很感兴趣),但是要跟踪各个单元((所以我花了很多年,但有人可能需要几天的时间,才能像你这样循环。我确实做了简化,并假设给定的费率是每年,但是您可以每天将其乘以365,或者将其每月乘以12,或者确保您的期间和天数具有相同的单位。
在同样的情况下,我选择跟随Double而不是BigDecimal代表钱是您领导并回答所问问题的一种选择。我不是在说我在这里回答的是最好的方法(可以通过使用Currency而不是假设以美元表示的货币来增强这种方法)。
一种不同的方法是使用指数来处理重复的乘法,或者即使不是,也可以简化for循环(这使您可以沿途执行诸如print语句之类的操作并允许对货币进行四舍五入)。 / p>
我没有解决潜在的增强功能,例如一年中不总是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);
}
}