我似乎无法弄清楚为什么数据出现错误。我认为我的方程式编码正确。任何帮助将不胜感激!
这是我的代码:
package loanmonthlycalc;
import java.util.Scanner;
public class LoanMonthlyCalc {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Get Data
System.out.println("Loan Monthly Payment Table");
System.out.print("Enter loan amount: \n");
double loanAmount = keyboard.nextDouble();
System.out.print("Enter rate range: \n");
double initialRate = keyboard.nextDouble();
double finalRate = keyboard.nextDouble();
System.out.print("Enter rate count: \n");
int rateCount = keyboard.nextInt();
//Print Table
System.out.println();
System.out.print("Year |");
double rate = initialRate;
double inc = ((finalRate - initialRate) / (rateCount - 1));
for (int count = 1; count <= rateCount; count++) {
String percent = "%";
System.out.printf("%8.2f%s ", rate * 100, percent);
rate = (rate + inc);
}//for each rate
System.out.print("\n-----|");
for (int count = 1; count <= rateCount; count++) {
System.out.print("-----------");
}
int constantA = 50000;
System.out.println();
if (loanAmount <= constantA) {
for (int year = 1; year <= 10; year++) {
System.out.printf("%4d |", year);
for (int count = 1; count <= rateCount; count++) {
int monthCount = (year * 12);
double monthlyRate = (rate / 12);
double rateFactor = (Math.pow(1 + monthlyRate, monthCount));
double monthPay = loanAmount * monthlyRate * rateFactor / (rateFactor -
System.out.printf("%10.2f", monthPay);
}//for
System.out.println();
}//for
}//if
if (loanAmount > constantA) {
for (int year = 5; year <= 30; year += 5) {
System.out.printf("%4d |", year);
for (int count = 1; count <= rateCount; count++) {
int monthCount = (year * 12);
double monthlyRate = (rate / 12);
double rateFactor = (Math.pow(1 + monthlyRate, monthCount));
double monthPay = loanAmount * monthlyRate * rateFactor / (rateFactor -
System.out.printf("%10.2f", monthPay);
}//for
System.out.println();
}//for
}//if
}//Main
}//LoanMonthlyCalc
and here is my output:
Loan Monthly Payment Table
Enter loan amount:
140000
Enter rate range:
.04 .1
Enter rate count:
5
Year | 4.00% 5.50% 7.00% 8.50% 10.00%
-----|-------------------------------------------------------
5 | 3078.97 3078.97 3078.97 3078.97 3078.97
10 | 1968.34 1968.34 1968.34 1968.34 1968.34
15 | 1635.47 1635.47 1635.47 1635.47 1635.47
20 | 1493.00 1493.00 1493.00 1493.00 1493.00
25 | 1423.06 1423.06 1423.06 1423.06 1423.06
30 | 1386.41 1386.41 1386.41 1386.41 1386.41
该表应如下所示:
Year | 4.00% 5.50% 7.00% 8.50% 10.00%
-----|------------------------------------------------------------
5 | 2578.31 2674.16 2772.17 2872.31 2974.59
10 | 1417.43 1519.37 1625.52 1735.80 1850.11
15 | 1035.56 1143.92 1258.36 1378.64 1504.45
20 | 848.37 963.04 1085.42 1214.95 1351.03
25 | 738.97 859.72 989.49 1127.32 1272.18
30 | 668.38 794.90 931.42 1076.48 1228.60
该作业给出的 2 个方程是:
月供 = 贷款金额 * 月利率 * 利率因子 / (利率因子-1)
其中,费率因子 = (1+月费率)^(月数)
我假设:
月费率 = 费率/12
和
月数 = (年 * 12)