gpt4 book ai didi

java - java 中的贷款摊销时间表及时间表日期

转载 作者:行者123 更新时间:2023-11-30 07:27:59 24 4
gpt4 key购买 nike

我试图在我的贷款时间表中包含时间表日期,但日期是错误的,因为它们跳过了一些月份:

 public static void printAmortizationSchedule(double principal, double annualInterestRate, int numYears) {
double interestPaid, principalPaid, newBalance;
double monthlyInterestRate, monthlyPayment;
int month;
int numMonths = numYears * 12;

// Output monthly payment and total payment
monthlyInterestRate = annualInterestRate / 12;
monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears);
System.out.format("Monthly Payment: %8.2f%n", monthlyPayment);
System.out.format("Total Payment: %8.2f%n", monthlyPayment * numYears * 12);

// Print the table header
printTableHeader();
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
for (month = 1; month <= numMonths; month++) {
// Compute amount paid and new balance for each payment period
interestPaid = principal * (monthlyInterestRate / 100);
principalPaid = monthlyPayment - interestPaid;
newBalance = principal - principalPaid;
cal.add(Calendar.MONTH, month);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-YYYY", Locale.getDefault());
String dddate = sdf.format(cal.getTime());
// Output the data item
printScheduleItem(month, dddate, interestPaid, principalPaid, newBalance);

// Update the balance
principal = newBalance;
}
}

打印时间表方法:

 private static void printScheduleItem(int month, String dddate, double interestPaid, double principalPaid, double newBalance) {
System.out.format("%8d%12s%10.2f%10.2f%12.2f\n",
month, dddate, interestPaid, principalPaid, newBalance);
}

我确定问题是在将月份添加到日期时出现的,我该如何解决这个问题?

最佳答案

你的错误在这里:

cal.add(Calendar.MONTH, month);

应该是:

cal.add(Calendar.MONTH, 1);

关于java - java 中的贷款摊销时间表及时间表日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com