我正试图使用嵌套的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;
}
}
所以我这样解决了它:
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;
}
}