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

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

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(&quot;&quot;);
System.out.printf(&quot;%nInterest Rate: %.2f%%&quot;, termSelection);
System.out.printf(&quot;%n%53s&quot;, &quot;Monthly Payment Schedule&quot;);
termSelection /= 100;
//monhtly payment schedule body
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;);
do{
System.out.printf(&quot;%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f&quot;, 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 &lt;= 12); 
System.out.println(&quot;&quot;);
for (int count = 0; count &lt;= 80; count++){
System.out.print(&#39;=&#39;);
}
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

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(&quot;%n%5s%14.2f%15.2f%15.2f%15.2f%15.2f&quot;, month, principal, monthlyPayment, monthlyPrincipal, monthlyInterest, closingBalance);
principal = closingBalance;
totalPrincipal += monthlyPrincipal;
totalInterest += monthlyInterest;
monthlyInterest = principal * effectiveMonthlyRate;
monthlyPrincipal = monthlyPayment - monthlyInterest;
closingBalance = principal - monthlyPrincipal;
month++;
} 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:

确定