英文:
Cannot figure out why loop isn't getting the correct sum
问题
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("\n");
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("\n");
for (int count = 0; count <= 80; count++){
System.out.print('=');
}
System.out.printf("%n%5s%44.2f%15.2f", "Ttls", totalPrincipal, totalInterest);
英文:
I'm trying to make a mortgage calculator, and for whatever reason it's getting the wrong sum for the principal and the interest. Is it possible that because it's being added up inside of the do-while loop that it gets it wrong? When I run the program it comes up with 8233.68 for the principal and 11423.59 for interest. If you sum them up on a calculator, it should be 8209.30 for principal and 11447.97 for the interest.
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);
答案1
得分: 1
抱歉,你提供的内容似乎有点混乱。你想要我翻译的部分是什么?请提供需要翻译的具体文本内容。
英文:
The problem is that you are doing calculation before you even enter the loop
double closingBalance = (principal - monthlyPrincipal);
should be
double closingBalance = (principal);
output
Ttls 8209.30 11447.97
答案2
得分: -1
你在循环的第一次迭代中重新计算了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);
英文:
You recalculate initial values of monthlyInterest and monthlyPrincipal on first iteration of the loop and then add them to totals. Add them to totals first and then recalculate.
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论