Java 贷款摊销

标签 java

请问我的代码有什么问题。它是一种迭代方法: 给定贷款的每月付款支付本金和利息。每月利息是通过将每月利率乘以余额(剩余本金)来计算的。 因此,当月支付的本金是每月还款额减去 月利息。编写一个程序,让用户输入贷款金额、年限和利率,并显示贷款的分期偿还时间表。 但是,我不断得到 NaN 只是为了计算每月付款。代码如下:

import java.util.Scanner;
public class Amortization {
public static void main(String[] args) {
//create Scanner 
Scanner s = new  Scanner(System.in);
//prompt Users  for  input
System.out.print("Enter loan Amount:");
int  loanAmount = s.nextInt();
System.out.print("Enter numberof Years:");
int numberYear =s.nextInt();
System.out.print("Enter Annual Interest Rate:");
int annualRate = s.nextInt();

double monthlyrate= annualRate/1200;
double monthlyPayment = loanAmount*monthlyrate/(1 -1/Math.pow(1+monthlyrate,numberYear*12));


System.out.printf("%6.3f",monthlyPayment);


// TODO code application logic here
}

最佳答案

我刚刚为类似的问题编写了代码。我与你分享我的解决方案。 我从http://java.worldbestlearningcenter.com/2013/04/amortization-program.html得到了很多想法

public class LoanAmortizationSchedule {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        // Prompt the user for loan amount, number of years and annual interest rate

        System.out.print("Loan Amount: ");
        double loanAmount = sc.nextDouble();

        System.out.print("Number of Years: ");
        int numYears = sc.nextInt();

        System.out.print("Annual Interest Rate (in %): ");
        double annualInterestRate = sc.nextDouble();

        System.out.println();  // Insert a new line

        // Print the amortization schedule

        printAmortizationSchedule(loanAmount, annualInterestRate, numYears);
    }

    /**
     * Prints amortization schedule for all months.
     * @param principal - the total amount of the loan
     * @param annualInterestRate in percent
     * @param numYears
     */
    public static void printAmortizationSchedule(double principal, double annualInterestRate,
                                                 int numYears) {
        double interestPaid, principalPaid, newBalance;
        double monthlyInterestRate, monthlyPayment;
        int month;
        int numMonths = numYears * 12;

        // Output monthly payment and total payment
        monthlyInterestRate = annualInterestRate / 12;
        monthlyPayment      = monthlyPayment(principal, monthlyInterestRate, numYears);
        System.out.format("Monthly Payment: %8.2f%n", monthlyPayment);
        System.out.format("Total Payment:   %8.2f%n", monthlyPayment * numYears * 12);

        // Print the table header
        printTableHeader();

        for (month = 1; month <= numMonths; month++) {
            // Compute amount paid and new balance for each payment period
            interestPaid  = principal      * (monthlyInterestRate / 100);
            principalPaid = monthlyPayment - interestPaid;
            newBalance    = principal      - principalPaid;

            // Output the data item
            printScheduleItem(month, interestPaid, principalPaid, newBalance);

            // Update the balance
            principal = newBalance;
        }
    }

    /**
     * @param loanAmount
     * @param monthlyInterestRate in percent
     * @param numberOfYears
     * @return the amount of the monthly payment of the loan
     */
    static double monthlyPayment(double loanAmount, double monthlyInterestRate, int numberOfYears) {
        monthlyInterestRate /= 100;  // e.g. 5% => 0.05
        return loanAmount * monthlyInterestRate /
                ( 1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12) );
    }

    /**
     * Prints a table data of the amortization schedule as a table row.
     */
    private static void printScheduleItem(int month, double interestPaid,
                                          double principalPaid, double newBalance) {
        System.out.format("%8d%10.2f%10.2f%12.2f\n",
            month, interestPaid, principalPaid, newBalance);
    }

    /**
     * Prints the table header for the amortization schedule.
     */
    private static void printTableHeader() {
        System.out.println("\nAmortization schedule");
        for(int i = 0; i < 40; i++) {  // Draw a line
            System.out.print("-");
        }
        System.out.format("\n%8s%10s%10s%12s\n",
            "Payment#", "Interest", "Principal", "Balance");
        System.out.format("%8s%10s%10s%12s\n\n",
            "", "paid", "paid", "");
    }
}

关于Java 贷款摊销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834825/

相关文章:

java - 如何在GET请求中传递postman中的列表并进入GetMapping

java - 在我的 fragment 中,非法状态异常显示在我的文档引用 firestore 集合中

java - fragment 集可见性空指针

java导入 "cannot find symbol"

java - 从 Spring+Mongo 中的文档数组中删除项目

java - 如果我用对象字段填充列表,当列表保留在范围内时,我的对象是否可以被垃圾回收?

java - Spring mvc :annotation-driven 问题

java - Spring MVC : how to implement DAO from custom interface

java - 在 JSF 中,被调用的 EJB 抛出的自定义异常被视为 EJBTransactionRolledBackException 或 NullPointerException 或 ServletException

JAVA HttpClient with php and Json parsing (not for Android)