【问题标题】:How to display mortgage payment with a nested for-loop?如何使用嵌套的 for 循环显示抵押贷款付款?
【发布时间】:2023-03-08 08:41:02
【问题描述】:

我正在尝试使用嵌套的 for 循环显示抵押贷款程序的表格。外循环由利率控制,因为它从起始利率处理到结束,而内循环由以年为单位的开始期限到以年为单位的结束期限控制。 每次迭代后利率增加0.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

标签: java for-loop multiple-columns


【解决方案1】:

所以我这样解决了:

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

      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2015-06-30
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多