Java 为什么我的数据打印不正确?付款计算器表

Java 为什么我的数据打印不正确?付款计算器表,java,Java,我似乎不明白为什么数据会出错。我认为我的方程式编码正确。任何帮助都将不胜感激 这是我的密码: package loanmonthlycalc; import java.util.Scanner; public class LoanMonthlyCalc { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //Get Data System.ou

我似乎不明白为什么数据会出错。我认为我的方程式编码正确。任何帮助都将不胜感激

这是我的密码:

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
本作业给出的两个方程式为:

月付款=贷款金额*月利率*利率系数/(利率系数-1)

其中,费率系数=(1+月费率)^(月计数)

我假设:

月费率=费率/12


月份计数=(年份*12)

注意缺少的
%

改变

 System.out.printf("10.2f", monthPay);

将if从中移出,如下所示:

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

if(loanAmount修复了其中的一部分,但它仍然没有正确排列!为什么constantA
50000
50000
?B/c是分配的一部分。如果贷款金额小于或等于50000,则以1年为增量从1年到10年。如果贷款金额大于50000,则以5年为增量从5年到30年但是在第一个for循环中,
%4d |
没有打印,因为
loanAmount
比n
constantA
更大,那么它不应该转到第二个循环吗?
  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