我似乎无法弄清楚数据出错的原因。我认为我的方程编码正确。任何帮助将不胜感激!
这是我的代码:
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)
答案 0 :(得分:1)
请注意缺少的%
更改
System.out.printf("10.2f", monthPay);
到
System.out.printf("%10.2f", monthPay);
将if移出for for,如下所示:
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 - 1);
System.out.printf("%10.2f", monthPay);
}//for
System.out.println();
}//for
}//if