【问题标题】:How do I solve this error for an undefined method?如何为未定义的方法解决此错误?
【发布时间】:2013-10-31 23:58:43
【问题描述】:

这就是我正在使用的。这是一个月供贷款计算器。我不断收到错误消息“方法monthlyPayment (double, int, int) 未定义类型Assignment 8。”此错误显示在 main 方法中。错误在第 27 行。

类

public class LoanCalc {
public static double monthlyPayment(double amountBorrowed, int loanLength, int intRate) {
        double principal;
        double interestRate;
        double monthlyPayment;

        principal = amountBorrowed;
        interestRate = intRate / 100 / 12;

        monthlyPayment = (interestRate * principal) /
                (1- Math.pow((1 + interestRate) , - loanLength * 12 ));



        return monthlyPayment;
    }
}

主要方法

1    import java.util.Scanner;
2    
3    public class Assignment8 {
4    
5       public static void main(String[] args) {
6           
7           Scanner kbd = new Scanner(System.in);
8           
9           System.out.println("Enter the amount borrowed: ");
10          double amountBorrowed = kbd.nextDouble();
11          
12          System.out.println("Enter the interest rate: ");
13          int intRate = kbd.nextInt();
14          
15          System.out.println("Enter the minimum length of loan: ");
16          int minLength = kbd.nextInt();
17          
18          System.out.println("Enter the maximum length of loan: ");
19          int loanLength = kbd.nextInt();
20          while (loanLength < minLength) {
21              System.out.println("Invalid input: Input must be greater than      22                       minimum length of loan");
23              System.out.println("Enter the maximum length of loan: ");
24              loanLength = kbd.nextInt();
25          }
26          
27          double payment = monthlyPayment(amountBorrowed, loanLength, intRate);
28          System.out.println(payment);
29          
30      }

    }

【问题讨论】:

    标签: java


    【解决方案1】:

    改成

    double payment = LoanCalc.monthlyPayment(amountBorrowed, loanLength, intRate);
    

    这是因为monthlyPayment()属于LoanCalc,而不是Assignment8,所以需要明确说明在哪里可以找到monthlyPayment()。

    【讨论】:

      【解决方案2】:

      你必须调用函数使用

      LoanCalc.monthlyPayment( ... )
      

      因为它是属于另一个类的静态方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        • 2022-07-03
        • 2018-10-08
        • 2021-12-30
        • 2021-12-04
        相关资源
        最近更新 更多