gpt4 book ai didi

java - 贷款支付计算器,如何实现多种方法。无法开发与主要方法交互的方法

转载 作者:行者123 更新时间:2023-11-29 05:36:51 25 4
gpt4 key购买 nike

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

我在编辑器中没有收到任何错误,但我的程序只要求用户提供三个输入,而不打印贷款报表。有什么建议吗?

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

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

最佳答案

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

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

}//end main
}//end class

main 方法引用的类方法也必须在同一个类中并且是static。一旦您开始构建自己的类和对象,情况就不会总是这样了

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

要使用您的方法捕获 double,只需在您的 main 方法中调用如下内容:

double answer = CalcMonthlyInterestRate(/*some double variable here*/); //in main

关于java - 贷款支付计算器,如何实现多种方法。无法开发与主要方法交互的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19215592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com