简体   繁体   English

如何使用嵌套的 for 循环显示抵押付款?

[英]How to display mortgage payment with a nested for-loop?

I am trying to display a table for a mortgage loan program using nested for-loops.我正在尝试使用嵌套的 for 循环显示抵押贷款计划的表格。 The outer loop is controlled by the interest rate as it processes it from the starting rate to the endinging, and the inner loop is controlled by the starting term in years to the ending term in years.外环由利率控制,从起始利率到期末处理,内环由起始年限到年终期控制。 The interest rate is incremented by .25 after every iteration and the year is incremented by 5.每次迭代后利率增加 0.25,年份增加 5。

I am having difficulties calculating the payment when more than a single column is generated.当生成多于一列时,我在计算付款时遇到困难。

This is the expected output:这是预期的输出:

Interest    15 Years        20 Years        25  Years
 4.000       739.69          605.98          527.84 
 4.250       752.28          619.23          541.74 
 4.500       764.99          632.65          555.83 
 4.750       777.83          646.22          570.12 
 5.000       790.79          659.96          584.59 

This is my output:这是我的输出:

Interest
  Rate     15  Years        20  Years        25  Years
 4.000       739.69          739.69          739.69 
 4.250       752.28          752.28          752.28 
 4.500       764.99          764.99          764.99 
 4.750       777.83          777.83          777.83 
 5.000       790.79          790.79          790.79 

and this is what I have to calculate the loan amount:这就是我必须计算的贷款金额:

//startingYear is the first term entered by the user
//ending Year is the last term entered by the user
     displayTableHeader(startingYear, endingYear, startCount, yearCounter);



//sCountaRate is the starting interest rate
// n determines how many time the rate will iterate
        for (int j = sCountRate;j <= n; j++){


//displays first column interest rate
          System.out.printf("\n %1.3f",startRate);


//startCount is the starting year
//ending year is the last term in years entered by user
//yearsCounter determines the amount of time it will increment (5 in this case)
          for (int i = startCount; i <= endingYear;i = i + yearCounter){



//calculates loan payment
            totalPayment = calculateLoanPayment(startRate, loanAmount, startCount); 


            System.out.printf("\t     %.2f ", totalPayment);

          }
//starting interest rate is incremented by .25 after every iteration
          startRate += TF_INCREMENTS;
        }

      }

So I solved it like this:所以我是这样解决的:

  displayTableHeader(startingYear, endingYear, startingYearDisplay, yearCounter);


        for (int j = sCountRate;j <= n; j++){

          System.out.printf("\n %1.3f",startRate);

          for (int i = startingYear; i <= endingYear;i = i + yearCounter){


            totalPayment = calculateLoanPayment(startRate, loanAmount, startingYearDisplay); 


            System.out.printf("\t%4.2f ", totalPayment);

 //I added this to add 5 every time the inner loop iterates, so it would do the first row.
            startingYearDisplay += 5;

          }
          startRate += TF_INCREMENTS;

//AND this to prevent it of increasing 5 every time a row was printed, so it wont go over the selected amount years.
          startingYearDisplay = startingYear;
        }

      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM