1

这个分配的想法是让多个方法相互交互。我向用户询问贷款金额、利率和贷款期限。然后程序应该有一种计算月利率的方法,一种计算并返回每月付款的方法和一种打印贷款报表的方法(借来的金额、年利率、月数和每月付款)。

我在编辑器中没有收到任何错误,但我的程序只要求用户输入三个输入并且不打印贷款声明。有什么建议么?

public class CarLoan {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // declare variables for main method
    double loanAmount;//double value loan amount 
    double annualInterestRate;//double value interest rate
    int numberOfMonths;//int value for number of months
    double monthlyPayment;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter the amount of your loan.");
    loanAmount = keyboard.nextDouble();

    System.out.println("Please enter the annual interest rate as a decimal. Ex. 7.5% = .075");
    annualInterestRate = keyboard.nextDouble();

    System.out.println("Please enter the number of months you have to pay back your loan.");
    numberOfMonths = keyboard.nextInt();

}

public static double calcMonthlyInterestRate(double annualInterestRate){
    double monthlyInterestRate;
        monthlyInterestRate = (annualInterestRate/12);
        return monthlyInterestRate;
}//end method CalcMonthlyInterestRate

    public static double calcMonthlyPayment(double monthlyInterestRate, double loanAmount, int            numberOfMonths     ){
    double monthlyPayment;
    double calcMonthlyPayment;
        calcMonthlyPayment = (monthlyInterestRate*loanAmount)/(1-(1+monthlyInterestRate)-numberOfMonths);
        return monthlyPayment = calcMonthlyPayment;
}//end method CalcMonthlyPayment

public static void loanStatment (double loanAmount, double annualInterestRate, intnumberOfMonths,  double monthlyPayment){
 System.out.println("Your loan amount is " +loanAmount);
 System.out.println(annualInterestRate);
 System.out.println(numberOfMonths);
 System.out.println(monthlyPayment);
  }

 }//end main method


}//end main method

我不确定我的某些代码是否仍然是多余的。

4

3 回答 3

2

由于 main 方法是静态的,并且您的 CalcMonthlyInterestRate 引用了您的 main 方法,因此 CalcMonthlyInterestRate 也必须是静态的,以便两者static reference相互创建。

在您的帖子底部,我们看到:

}//end main
}//end class

由 main 方法引用的类方法也必须在其同一个类中并且存在static。一旦您开始构建自己的类和对象,情况并非总是如此

 }//end main  
       public static double CalcMonthlyInterestRate(double annualInterestRate) {
          double monthlyInterestRate;
          monthlyInterestRate = (annualInterestRate/12);
          return monthlyInterestRate;
       }
 }//end class

要使用您的方法捕获双精度,只需在您的 main 方法中调用类似的内容:

double answer = CalcMonthlyInterestRate(/*some double variable here*/); //in main
于 2013-10-07T00:53:25.393 回答
1

您的方法CalcMonthlyInterestRate需要在您的CarLoan班级内,而不是在班级外。

于 2013-10-07T00:42:25.337 回答
0

那是因为你有这样的:

import java.util.Scanner;//instance of scanner class for input from user

public class CarLoan {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // declare variables for main method
        double loanAmount;//double value loan amount 
        double annualInterestRate;//double value interest rate
        int numberOfMonths;//int value for number of months

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the amount of your loan.");
        loanAmount = keyboard.nextDouble();

        System.out.println("Please enter the annual interest rate as a decimal. Ex. 7.5% = .075");
        annualInterestRate = keyboard.nextDouble();

        System.out.println("Please enter the number of months you have to pay back your loan.");
        numberOfMonths = keyboard.nextInt();

    }
}//end main method


public static double CalcMonthlyInterestRate(double annualInterestRate)
{
    double monthlyInterestRate;
    monthlyInterestRate = (annualInterestRate/12);
    return monthlyInterestRate;
}

这里有几个问题......一个是//end main 方法并不是 main 方法的真正结束。你的就结束了。Java 中的所有方法都必须在一个类中。类代表对象,Java 是“面向对象的”。对象上的每个方法都代表该对象的“行为”。(大多数情况下)无意义的悬空行为是毫无意义的。

预期的“类、接口或枚举”意味着您当前正在输入代码的位置,您只能放置一个类、一个接口或一个枚举。这些是 Java 程序的顶级构造(除非您想获得包技术等...)

将函数放入类中(并修复一些代码冗余),我们有:

import java.util.Scanner;//instance of scanner class for input from user

public class CarLoan {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // declare variables for main method
        double loanAmount;//double value loan amount 
        double annualInterestRate;//double value interest rate
        int numberOfMonths;//int value for number of months

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the amount of your loan.");
        loanAmount = keyboard.nextDouble();

        System.out.println("Please enter the annual interest rate as a decimal. Ex. 7.5% = .075");
        annualInterestRate = keyboard.nextDouble();
        double monthlyInterestRate = CalcMonthlyInterestRate(annualInterestRate);

        System.out.println("Please enter the number of months you have to pay back your loan.");
        numberOfMonths = keyboard.nextInt();
    } //end main method

    public static double CalcMonthlyInterestRate(double annualInterestRate)
    {
        return annualInterestRate / 12;
    } //end monthly interest method

} //end class

此外,Java 中的方法通常以小写字母开头。

于 2013-10-07T00:54:21.177 回答