无法显示变量

时间:2013-10-31 01:51:36

标签: java variables methods

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

3 个答案:

答案 0 :(得分: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));

答案 1 :(得分:2)

monthlyPayment(double amountBorrowed, int loanLength, int percentage)

您需要传递参数

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

答案 2 :(得分:1)

试试这个

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

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

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