我对编程很陌生,在我的主要方法中显示变量monthlyPayment 时遇到了一些麻烦;我认为这与以前的方法有关。这是一个每月付款计算器。
import java.util.Scanner;
public class assignment8 {
public static double pow(double a, int b) {
double ans = 1;
if (b < 0) {
for (int i = 0; i < -b; i++) {
ans *= 1/a;
}
}
return ans;
}
public static double monthlyPayment(double amountBorrowed, int loanLength, int percentage) {
double monthlyPayment;
double P = amountBorrowed;
double N = 12 * loanLength;
double r = (percentage / 100) / 12;
monthlyPayment = (r * P) / (1 - Math.pow((1 + r) , -N ));
return monthlyPayment;
}
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 interestRate = kbd.nextInt();
System.out.print("Enter the minimum length of the loan: ");
int minLoanLength = kbd.nextInt();
System.out.print("Enter the maximum length of the loan: ");
int maxLoanLength = kbd.nextInt();
while (maxLoanLength < minLoanLength) {
System.out.print("Enter the maximum legth og the loan: ");
maxLoanLength = kbd.nextInt();
}
for (int i = minLoanLength; i <= maxLoanLength; i++) {
System.out.println(i + monthlyPayment);
}
}
}