需要帮助Java摊销表

时间:2011-05-03 18:52:33

标签: java

我需要帮助来迭代计算并在循环中列出几个月。

我不知道如何使用更新的信息列出贷款余额。此代码列出了每个地方的项目,但每个数字与最后一个相同。第一个月的计算显示整个360个月。

用Java编写程序(没有图形用户界面)       贷款额为20万美元,利率为5.75%       和30年的任期。显示抵押付款金额然后       列出每笔付款的贷款余额和利息       贷款期限。如果列表将滚出屏幕,       使用循环显示部分列表,犹豫,       然后显示更多列表。

/ 声明第3周的所有变量 /

     double anualInterest = .0575;
     double interestCompoundedMonthly = 0;
     double interestForPeriod = 0;
     double principalAtEndOfPeriod = 200000.00;
     double portionToPrincipal = 0;
     double amountOfPaymentMonthly = 1167.15;
     double newPrincipalAtEndOfPeriod = 0;

/ 计算付款第3周 /

        interestCompoundedMonthly = (anualInterest/12); //.0575/12=.0047916

        interestForPeriod = interestCompoundedMonthly * principalAtEndOfPeriod; // 958.32 =.0049916*200,000

        portionToPrincipal = amountOfPaymentMonthly - interestForPeriod; // 208.83 = 1167.15-958.32

        newPrincipalAtEndOfPeriod = principalAtEndOfPeriod - portionToPrincipal; //199791.18 = 200000-208.83

        System.out.println (i+ "\t\t" + dcm.format(monthlyPayment)+"\t\t" +dcm.format(interestForPeriod)+"\t\t\t"+dcm.format(portionToPrincipal)+ "\t\t\t" +dcm.format(newPrincipalAtEndOfPeriod));  

提前感谢任何建议。

/****************
 * Week 2       *
 ****************/
   /*Monthly Payment Program
     A program written in Java (without a graphical user interface)
     that will calculate and display the monthly payment amount
     to fully amortize a $200,000.00 loan 
     over a 30 year term at 5.75‰ interest.*/

/****************
 * Week 3       *
 ****************/

  /* Write the program in Java (without a graphical user interface)
  using a loan amount of $200,000 with an interest rate of 5.75% 
  and a 30 year term. Display the mortgage payment amount and then
  list the loan balance and interest paid for each payment over 
  the term of the loan. If the list would scroll off the screen, 
  use loops to display a partial list, hesitate, 
  and then display more of the list.*/

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


  public class Monthly_Payment_Calculator {
  public static void main (String [] args)  {



  /*Declare all Variables Week 2*/

    /*Variables provided by customer*/

     double loanAmount = 200000.00;   // $ amount borrowed
     double interestRate = 5.75;     // interest rate 5.75%
     int    years = 30;             // years of loan

    /*Variables needed for calculating*/ 

     int    months = 0;            // months for calculating
     double monthlyPayment = 0;   // monthly payment for calculating
     double interest = 0;        // interest rate for calculating

    /*Declare all Variables for Week 3*/

     double anualInterest = .0575;
     double interestCompoundedMonthly = 0;
     double interestForPeriod = 0;
     double principalAtEndOfPeriod = 200000.00;
     double portionToPrincipal = 0;
     double amountOfPaymentMonthly = 1167.15;
     double newPrincipalAtEndOfPeriod = 0;

     /*Variables for storing previous balances*/       




     java.text.DecimalFormat dcm = new java.text.DecimalFormat("$,###.00");    
                                // format for currency


  /*Calculate Payment Week 2*/

     interest = interestRate / 100;

     months = years * 12;

     monthlyPayment = (loanAmount * (interest/12))/(1 - 1 /Math.pow((1 + interest/12), months));    


   /*Display the mortgage payment amount as per WK3 assignment*/

     System.out.println ("Total Monthly Payment is ");        
     System.out.println (dcm.format(monthlyPayment));           


  /*Display columns*/
     System.out.println("Month #\t Amount of Payment\tInterest for Period\tPortion to Principal\tPrincipal at End of Period\n");
     System.out.println("0\t\t\t0\t\t0\t\t\t0\t\t\t"+ dcm.format(principalAtEndOfPeriod));
     //Prints headers for columns


  /*Loop to calculate and print monthly payments*/

     for(int i=1; i <= months; i++)    // 360 months
     { 

     /*Calculate Payments Week 3*/


        interestCompoundedMonthly = (anualInterest/12); //.0575/12=.0047916

        interestForPeriod = interestCompoundedMonthly * principalAtEndOfPeriod; // 958.32 =.0049916*200,000

        portionToPrincipal = amountOfPaymentMonthly - interestForPeriod; // 208.83 = 1167.15-958.32

        newPrincipalAtEndOfPeriod = principalAtEndOfPeriod - portionToPrincipal; //199791.18 = 200000-208.83

        System.out.println (i+ "\t\t" + dcm.format(monthlyPayment)+"\t\t" +dcm.format(interestForPeriod)+"\t\t\t"+dcm.format(portionToPrincipal)+ "\t\t\t" +dcm.format(newPrincipalAtEndOfPeriod));  


     //recalculate interest for period
     //recalculate the  portion to principal
     //recalculate principal at end of period
     //set the remaining balance as the mortgage loan for the next repetition



        if(i%12==0 && i<months){


          /*Code to delay ending the program*/

           System.out.println( );
           System.out.println( );
           System.out.println ("(Please Press Enter to Continue the List)");
           System.out.println( );
           System.out.println( );
           System.out.println("Month #\t Amount of Payment\tInterest for Period\tPortion to Principal\tPrincipal at End of Period\n");
           try {
              System.in.read();  //Read input from the keyboard

           } 
              catch (IOException e) {  //Catch the input exception
                 return;  //and just return

              }
        }
     }
  }

}

2 个答案:

答案 0 :(得分:1)

您需要在付款计算结束时重置您的本金余额。尝试添加:

prindipalAtEndOfPeriod = newPrincipalAtEndOfPeriod;

我知道已经有一段时间了,但也许别人会觉得这很有帮助。

答案 1 :(得分:0)

我不确定你的兴趣是如何运作的,但我猜你正试图计算一个总和。 也许你可以在'for'循环中尝试以下内容:

// a is the cummulative sum
// b is the monthly calculation
a = a + b;