1

我对编程很陌生,在我的主要方法中显示变量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);
    }
}   
}
4

3 回答 3

2

这是你的monthlyPayment方法:

public static double monthlyPayment(double amountBorrowed, int loanLength, int percentage)

它需要 3 个参数并返回一个双精度值。

这就是您调用monthlyPayment方法的方式:

System.out.println(i + monthlyPayment);

你没有向它发送任何论据。你甚至不包括(). 你的编译器应该抱怨。

你需要这样做:

System.out.println(i + monthlyPayment(amountBorrowed, loanLength, percentage));

注意:您可能仍然无法获得预期的结果。这将汇总i和您调用的结果,monthlyPayment然后打印出来。你可能想要这样的东西:

System.out.println("Month " + i + " payment: " + monthlyPayment(amountBorrowed, loanLength, percentage));
于 2013-10-31T01:53:51.260 回答
2
monthlyPayment(double amountBorrowed, int loanLength, int percentage)

您需要传递参数

System.out.println(i + monthlyPayment( amountBorrowed, loanLength, percentage));
于 2013-10-31T01:54:13.217 回答
1

试试这个

System.out.println(i + ": " + monthlyPayment(amountBorrowed, loanLength, percentage));

iand的类型monthlyPayment是 int 和 double。默认情况下,2 号的+运算符将返回 2 号的总和。

在使用之前,您需要将数字转换为字符串+

于 2013-10-31T01:57:26.010 回答