需要Java摊销表计算帮助

问题描述 投票:0回答:3

这是我的作业。 我终于得到了正确显示的月份,但金额是错误的。 另外,没有必要,但最好在每次贷款之间停下来打印一些东西。 比如贷款1、贷款2、贷款3...

任何帮助将不胜感激

/*Write the program in Java (without a graphical user interface) 
and have it calculate the payment amount for 3 mortgage loans:

 - 7 year at 5.35%
 - 15 year at 5.5%
 - 30 year at 5.75%

Use an array for the different loans. 
Display the mortgage payment amount for each loan 
and then list the loan balance and interest paid for 
each payment over the term of the loan. 
Use loops to prevent lists from scrolling off the screen.*/

我的月份是正确的,但贷款金额是错误的。

    import java.io.IOException;      //Code that delays ending the program


   class MonthlyRhondav4
    {      
  
  public static void main ( String[] args) throws IOException{
     
     double loanAmount = 200000.00;   // $ amount borrowed
     double monthlyPayment = 0;   // monthly payment for calculating
     double loanBalance;
     double interestPaid;
     double principalPaid;
     int paymentCounter;
     int lineCounter = 0;
    

               
     java.text.DecimalFormat dcm = new java.text.DecimalFormat("$,###.00");                                      
  
            
     int termArray[] = {84, 180, 360};      // Different loan terms in months 
     double interestArray[] = {0.0535, 0.055, 0.0575};// Different interest rates for the loan
     int k =0;// gonna be paymentIndex
        
  /*Code to start the payment list*/   
   
     System.out.print("\n\nPlease Press Enter to Continue to the 3 Different    Amortization Lists");
     System.out.println ();
     System.out.println ();
  
     System.in.read();
     System.in.read();  
     
  /*Display columns*/    
    
     System.out.println("Month \t Loan Amount Left\tInterest\t\tPrincipal \n"); //Prints headers for columns
     System.out.println ();
                    
              
  /*Loop to calculate and print monthly payments*/
  
     //for(k=0; k<3; k++){// k is going to be paymentIndex to loop through index
     
     for (k = 0; k < interestArray.length; k++) {
     
        for(paymentCounter =1; paymentCounter <= termArray[k]; paymentCounter++)  // months through array
        { 
        
        /********TROUBLE HERE***************************************************************************************/
        
           monthlyPayment = ((loanAmount * (interestArray[k]) * termArray[k]) + loanAmount) / (termArray[k] * 12);

           interestPaid = loanAmount*(interestArray[k]/12);            //interest paid through array
           
           principalPaid = monthlyPayment-loanAmount*(interestArray[k]/12);       //principal paid
      
      /*need to fig monthly payment+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      
      
                   
          
           System.out.println(paymentCounter + "\t" + dcm.format(loanAmount) + "\t\t" + dcm.format(interestPaid) + "\t\t\t" + dcm.format(principalPaid));
        
           
           
           lineCounter++;                                //Increment the display counter
           if (lineCounter > 11 && paymentCounter < termArray[k]*12) //Check to see if 12             
           {
                   
              System.out.println ("Please Press Enter to Continue the List" ); //Code to delay ending the program
              System.in.read();
              System.in.read();
              lineCounter = 0; 
                         
           }
        
        }
        loanAmount = (loanAmount - (monthlyPayment-loanAmount*(interestArray[k]/12))); //Calculate new loan amount
     }
  
  
  }//ends public static void main 
     }//ends public class 
java math
3个回答
1
投票

一个观察——你没有对贷款余额做任何事情。 没有任何变化的原因是,在计算给定付款的利息和本金金额后,您并没有将贷款余额减少付款的本金部分。 您需要更改代码以显示当前贷款余额,并根据当前贷款余额而不是原始贷款金额计算本金/利息分割。


已编辑

好的 - 我看到您正在尝试更新余额,但您将其置于贷款循环之外。 它需要位于循环内部,以便每次付款都会更新。 此外,你还会一遍又一遍地遇到诸如

loanAmount * (interestArray[k] / 12)
之类的事情。 考虑使用变量,例如

double interestPaid = loanAmount * (interestArray[k] / 12)

这将使您的代码更易于阅读且更易于维护,因为如果您在计算中发现错误,您只需在一处修复错误,而不必在计算的所有地方修复它。

我也不明白你在哪里计算每月付款。 这是原始贷款金额、还款次数和利率的函数。 请记住,每月还款额是固定的,每笔还款额的利息/本金分配将随着贷款的偿还而变化。 您可能会发现 http://en.wikipedia.org/wiki/Mortgage_calculator 对于计算每月付款的公式很有用。


1
投票

首先:永远不要使用 double 来计算某些东西......这是你必须记住的建议。如果你不想相信我,请在谷歌上搜索“java doublecomputation”或类似的东西,你就会看到。或者阅读优秀书籍《Effective Java》

BigDecimal 类在 Java 中具有正确的数字


0
投票

很多错误。例如,您从未将

monthlyPayment
设置为 0 以外的任何值。您也不使用
loanBalance
loanAmount
应该是一个常数。您还可以简化冗余计算。示例:

 interestPaid = loanBalance*(interestArray[k]/12); 
 principalPaid = monthlyPayment-interestPaid; 

而不是

 interestPaid = loanBalance*(interestArray[k]/12); 
 principalPaid = monthlyPayment-loanBalance*(interestArray[k]/12);

我也不确定您是否拥有正确的利率公式,但在您删除一些更明显的错误之前我不会进行检查。

© www.soinside.com 2019 - 2024. All rights reserved.