如何显示带有嵌套for循环的抵押贷款付款?

问题描述 投票:-1回答:1

我正在尝试使用嵌套的for循环显示抵押贷款计划的表格。外部循环受利率控制,因为利率是从开始利率到结束期间的利率,内部循环受起始期限(以年为单位)到结束期限(以年为单位)的控制。每次迭代后,利率都增加.25,而年份则增加5。

当生成多列时,我很难计算付款。

这是预期的输出:

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 

这是我的输出:

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 

这是我必须计算的贷款金额:

//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;
        }

      }
java for-loop multiple-columns
1个回答
0
投票

所以我这样解决了:

  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;
        }

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