无法弄清楚为什么循环没有得到正确的总和。

huangapple go评论90阅读模式
英文:

Cannot figure out why loop isn't getting the correct sum

问题

  1. double termSelection = 0.0359;
  2. double principal = 325000;
  3. double effectiveMonthlyRate = 0.0029695338579054376;
  4. double monthlyPayment = 1638.1053796234314;
  5. double monthlyInterest = (principal * effectiveMonthlyRate);
  6. double monthlyPrincipal = (monthlyPayment - monthlyInterest);
  7. double closingBalance = (principal - monthlyPrincipal);
  8. int month = 1;
  9. double totalPrincipal = 0;
  10. double totalInterest = 0;
  11. termSelection *= 100;
  12. //monthly payment schedule header
  13. System.out.println("\n");
  14. System.out.printf("%nInterest Rate: %.2f%%", termSelection);
  15. System.out.printf("%n%53s", "Monthly Payment Schedule");
  16. termSelection /= 100;
  17. //monhtly payment schedule body
  18. System.out.printf("%n%5s%14s%15s%15s%15s%15s", "Month", "Open Bal", "Payment", "Princ", "Interest", "Closing Bal");
  19. do{
  20. System.out.printf("%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f", month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
  21. month++;
  22. principal = closingBalance;
  23. monthlyInterest = principal * effectiveMonthlyRate;
  24. monthlyPrincipal = monthlyPayment - monthlyInterest;
  25. closingBalance = principal - monthlyPrincipal;
  26. totalPrincipal = totalPrincipal + monthlyPrincipal;
  27. totalInterest = totalInterest + monthlyInterest;
  28. }
  29. while (month <= 12);
  30. System.out.println("\n");
  31. for (int count = 0; count <= 80; count++){
  32. System.out.print('=');
  33. }
  34. 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.

  1. double termSelection = 0.0359;
  2. double principal = 325000;
  3. double effectiveMonthlyRate = 0.0029695338579054376;
  4. double monthlyPayment = 1638.1053796234314;
  5. double monthlyInterest = (principal * effectiveMonthlyRate);
  6. double monthlyPrincipal = (monthlyPayment - monthlyInterest);
  7. double closingBalance = (principal - monthlyPrincipal);
  8. int month = 1;
  9. double totalPrincipal = 0;
  10. double totalInterest = 0;
  11. termSelection *= 100;
  12. //monthly payment schedule header
  13. System.out.println(&quot;&quot;);
  14. System.out.printf(&quot;%nInterest Rate: %.2f%%&quot;, termSelection);
  15. System.out.printf(&quot;%n%53s&quot;, &quot;Monthly Payment Schedule&quot;);
  16. termSelection /= 100;
  17. //monhtly payment schedule body
  18. System.out.printf(&quot;%n%5s%14s%15s%15s%15s%15s&quot;, &quot;Month&quot;, &quot;Open Bal&quot;, &quot;Payment&quot;, &quot;Princ&quot;, &quot;Interest&quot;, &quot;Closing Bal&quot;);
  19. do{
  20. System.out.printf(&quot;%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f&quot;, month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
  21. month++;
  22. principal = closingBalance;
  23. monthlyInterest = principal * effectiveMonthlyRate;
  24. monthlyPrincipal = monthlyPayment - monthlyInterest;
  25. closingBalance = principal - monthlyPrincipal;
  26. totalPrincipal = totalPrincipal + monthlyPrincipal;
  27. totalInterest = totalInterest + monthlyInterest;
  28. }
  29. while (month &lt;= 12);
  30. System.out.println(&quot;&quot;);
  31. for (int count = 0; count &lt;= 80; count++){
  32. System.out.print(&#39;=&#39;);
  33. }
  34. System.out.printf(&quot;%n%5s%44.2f%15.2f&quot;, &quot;Ttls&quot;, totalPrincipal, totalInterest);

答案1

得分: 1

抱歉,你提供的内容似乎有点混乱。你想要我翻译的部分是什么?请提供需要翻译的具体文本内容。

英文:

The problem is that you are doing calculation before you even enter the loop

  1. double closingBalance = (principal - monthlyPrincipal);

should be

  1. double closingBalance = (principal);

output

  1. Ttls 8209.30 11447.97

答案2

得分: -1

你在循环的第一次迭代中重新计算了monthlyInterest和monthlyPrincipal的初始值,然后将它们添加到总数中。请先将它们添加到总数中,然后再重新计算。

  1. do {
  2. System.out.printf("%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f", month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
  3. principal = closingBalance;
  4. totalPrincipal += monthlyPrincipal;
  5. totalInterest += monthlyInterest;
  6. monthlyInterest = principal * effectiveMonthlyRate;
  7. monthlyPrincipal = monthlyPayment - monthlyInterest;
  8. closingBalance = principal - monthlyPrincipal;
  9. month++;
  10. } 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.

  1. do {
  2. System.out.printf(&quot;%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f&quot;, month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
  3. principal = closingBalance;
  4. totalPrincipal += monthlyPrincipal;
  5. totalInterest += monthlyInterest;
  6. monthlyInterest = principal * effectiveMonthlyRate;
  7. monthlyPrincipal = monthlyPayment - monthlyInterest;
  8. closingBalance = principal - monthlyPrincipal;
  9. month++;
  10. } while (month &lt;= 12);

huangapple
  • 本文由 发表于 2020年10月23日 07:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64491867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定