Java 贷款摊销

问题描述 投票:0回答:3

请问我的代码可能有什么问题。这是一种迭代方法: 每月支付给定贷款的本金和利息。月利息由月利率乘以余额(剩余本金)计算得出。 因此,当月支付的本金是每月还款额减去 每月利息。编写一个程序,让用户输入贷款金额、年数和利率,并显示贷款的摊销时间表。 但是,我一直得到 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
}

}

java
3个回答
5
投票

我刚刚为类似的问题编写了代码。我与你分享我的解决方案。 我从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", "");
    }
}

1
投票

那是因为您输入了数字,然后输入了 Enter 。因此,您的 nextLine 方法调用仅读取返回键,而 nextInt 仅读取整数值,忽略返回键。为了避免这个问题:

读取输入后,您可以调用类似以下内容:

int loanAmount=s.nextInt();
s.nextLine();//to read the return key.

此外,格式化代码(标识)可能是个好主意


-2
投票

贷款摊销:在金融领域,贷款摊销是通过一系列固定定期付款偿还贷款的过程,通常包括本金和利息。 Java 实现:在 Java 中实现贷款摊销需要创建一个计算特定期限内贷款支付的程序。 输入参数:程序应采用输入参数,例如贷款金额、年利率和贷款期限(以年为单位)。 每月还款额计算:根据年利率计算每月利率,并使用它与贷款期限一起使用以下公式计算每月还款额:= ( 磷 × r 1 - ( 1 + r ) - n ) 中号=( 1−(1+r) −n

P×r )在哪里 磷 P为贷款金额, r r 是月利率,并且 n n 是付款总数。 摊销时间表:创建摊销时间表,详细说明每笔付款的利息和本金组成部分。 付款细目:随着贷款的支付,利息部分减少,而本金部分随着后续的每次付款而增加。 剩余余额:计算每次付款后的剩余余额并显示在明细表中。 最终付款:确保最终付款清除所有剩余余额,并在必要时进行调整。 Java 库:利用 Java 的内置库,例如用于精确算术的 BigDecimal 和用于数据结构的 java.util。 验证:包括错误处理和输入验证,以确保准确高效的处理。 输出格式:以清晰、专业的格式呈现摊销时间表,显示付款编号、日期、每月付款、利息、本金和余额。 用户界面:实现用户友好的界面(基于控制台或 GUI),以便于与贷款摊销计划进行交互。

© www.soinside.com 2019 - 2024. All rights reserved.