我创建了一个贷款支付计算器。每当我输入任何输入时,输出都会显示为 NaN。关于我可以改变的任何想法。第一段代码是main方法。第二个是另一个类。
主要方法
import java.util.Scanner;
public class Assignment8 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("Enter the amount borrowed: ");
double amountBorrowed = kbd.nextDouble();
System.out.print("Enter the interest rate: ");
int intRate = kbd.nextInt();
System.out.print("Enter the minimum length of loan: ");
int minLength = kbd.nextInt();
System.out.print("Enter the maximum length of loan: ");
int loanLength = kbd.nextInt();
while (loanLength < minLength) {
System.out.println("Invalid input: Input must be greater than minimum length of loan");
System.out.println("Enter the maximum length of loan: ");
loanLength = kbd.nextInt();
}
for (int i = minLength; i <= loanLength; i++) {
double payment = LoanCalc.monthlyPayment(amountBorrowed,loanLength, intRate);
System.out.println(payment);
}
}
}
班级
public class LoanCalc {
public static double monthlyPayment(double amountBorrowed, int loanLength, int intRate) {
double interestRate;
double monthlyPayment;
interestRate = intRate / 100 / 12;
monthlyPayment = (interestRate * amountBorrowed) / (1- Math.pow((1 + interestRate) , - loanLength * 12 ));
return monthlyPayment;
}
}