无法弄清楚为什么循环没有得到正确的总和
Cannot figure out why loop isn't getting the correct sum
我正在尝试制作一个抵押贷款计算器,但无论出于何种原因,本金和利息的总和是错误的。有没有可能是因为它是在 do-while 循环中添加的,所以它出错了?当我 运行 程序时,本金为 8233.68,利息为 11423.59。如果用计算器加起来的话,本金应该是8209.30,利息应该是11447.97
double termSelection = 0.0359;
double principal = 325000;
double effectiveMonthlyRate = 0.0029695338579054376;
double monthlyPayment = 1638.1053796234314;
double monthlyInterest = (principal * effectiveMonthlyRate);
double monthlyPrincipal = (monthlyPayment - monthlyInterest);
double closingBalance = (principal - monthlyPrincipal);
int month = 1;
double totalPrincipal = 0;
double totalInterest = 0;
termSelection *= 100;
//monthly payment schedule header
System.out.println("");
System.out.printf("%nInterest Rate: %.2f%%", termSelection);
System.out.printf("%n%53s", "Monthly Payment Schedule");
termSelection /= 100;
//monhtly payment schedule body
System.out.printf("%n%5s%14s%15s%15s%15s%15s", "Month", "Open Bal", "Payment", "Princ", "Interest", "Closing Bal");
do{
System.out.printf("%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f", month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
month++;
principal = closingBalance;
monthlyInterest = principal * effectiveMonthlyRate;
monthlyPrincipal = monthlyPayment - monthlyInterest;
closingBalance = principal - monthlyPrincipal;
totalPrincipal = totalPrincipal + monthlyPrincipal;
totalInterest = totalInterest + monthlyInterest;
}
while (month <= 12);
System.out.println("");
for (int count = 0; count <= 80; count++){
System.out.print('=');
}
System.out.printf("%n%5s%44.2f%15.2f", "Ttls", totalPrincipal, totalInterest);
您在循环的第一次迭代中重新计算 monthlyInterest 和 monthlyPrincipal 的初始值,然后将它们添加到总计中。先将它们加到总数中,然后重新计算。
do {
System.out.printf("%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f", month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
principal = closingBalance;
totalPrincipal += monthlyPrincipal;
totalInterest += monthlyInterest;
monthlyInterest = principal * effectiveMonthlyRate;
monthlyPrincipal = monthlyPayment - monthlyInterest;
closingBalance = principal - monthlyPrincipal;
month++;
} while (month <= 12);
问题是你还没进入循环就开始计算了
double closingBalance = (principal - monthlyPrincipal);
应该是
double closingBalance = (principal);
输出
Ttls 8209.30 11447.97
我正在尝试制作一个抵押贷款计算器,但无论出于何种原因,本金和利息的总和是错误的。有没有可能是因为它是在 do-while 循环中添加的,所以它出错了?当我 运行 程序时,本金为 8233.68,利息为 11423.59。如果用计算器加起来的话,本金应该是8209.30,利息应该是11447.97
double termSelection = 0.0359;
double principal = 325000;
double effectiveMonthlyRate = 0.0029695338579054376;
double monthlyPayment = 1638.1053796234314;
double monthlyInterest = (principal * effectiveMonthlyRate);
double monthlyPrincipal = (monthlyPayment - monthlyInterest);
double closingBalance = (principal - monthlyPrincipal);
int month = 1;
double totalPrincipal = 0;
double totalInterest = 0;
termSelection *= 100;
//monthly payment schedule header
System.out.println("");
System.out.printf("%nInterest Rate: %.2f%%", termSelection);
System.out.printf("%n%53s", "Monthly Payment Schedule");
termSelection /= 100;
//monhtly payment schedule body
System.out.printf("%n%5s%14s%15s%15s%15s%15s", "Month", "Open Bal", "Payment", "Princ", "Interest", "Closing Bal");
do{
System.out.printf("%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f", month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
month++;
principal = closingBalance;
monthlyInterest = principal * effectiveMonthlyRate;
monthlyPrincipal = monthlyPayment - monthlyInterest;
closingBalance = principal - monthlyPrincipal;
totalPrincipal = totalPrincipal + monthlyPrincipal;
totalInterest = totalInterest + monthlyInterest;
}
while (month <= 12);
System.out.println("");
for (int count = 0; count <= 80; count++){
System.out.print('=');
}
System.out.printf("%n%5s%44.2f%15.2f", "Ttls", totalPrincipal, totalInterest);
您在循环的第一次迭代中重新计算 monthlyInterest 和 monthlyPrincipal 的初始值,然后将它们添加到总计中。先将它们加到总数中,然后重新计算。
do {
System.out.printf("%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f", month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
principal = closingBalance;
totalPrincipal += monthlyPrincipal;
totalInterest += monthlyInterest;
monthlyInterest = principal * effectiveMonthlyRate;
monthlyPrincipal = monthlyPayment - monthlyInterest;
closingBalance = principal - monthlyPrincipal;
month++;
} while (month <= 12);
问题是你还没进入循环就开始计算了
double closingBalance = (principal - monthlyPrincipal);
应该是
double closingBalance = (principal);
输出
Ttls 8209.30 11447.97