我正在做课堂作业,我们刚刚开始制定自己的方法,而我认为似乎很容易的事情变得非常沮丧,希望你能帮助我解决这个问题。
首先要做的事情是我要完成的任务是:制作一个模块化程序来计算每月付款,看起来很容易,但这个问题的一些限制如下
主要方法应该:
询问用户
- 贷款金额
- 年利率(以小数计,7.5% 为 0.075)
- 月数
和
- 调用方法计算并返回月利率(年利率/12)
- 调用方法计算并返回每月付款
- 调用一个方法来打印显示借款金额、年利率、月数和每月付款的贷款报表。
我已经完成了打印贷款报表的工作,但我无法以正确的方式调用它,并在我运行程序后让它显示出来:/所以如果你能帮助我了解它是如何完成的,我将不胜感激。
(我意识到我的代码中可能还有其他错误,但现在我宁愿只关注我需要完成的事情) import java.util.Scanner; 公共类 LoanPayment {
/**
* The main method declares the variables in program while getting the user
* info of amount loaned, interest rate of the loan, and the loans duration.
*
* The main method also calls other methods to calculate monthly interest
* monthly payments and the output of the loan statement
*/
public static void main(String[] args)
{
// declare variables
double interest; // interest attributed to the loan
double mInterest; // loans interest divided by 12
int time; // how long the loan was taken out for
double principle; // the amount borrowed
double mPayment; // how much is to be paid each month
double loan;
// initate new scanner class
Scanner keyboard = new Scanner(System.in);
// get user input/information
System.out.println("Hi, Please enter the loan amount here:");
principle = keyboard.nextDouble();
System.out.println("Thanks, now what is the annual interest rate in decimal notation" +
"(example: 7.5% is 0.075:");
interest = keyboard.nextDouble();
System.out.println("now please put in the number of months the loan was taken out for");
time = keyboard.nextInt();
// call method to calculate and return monthly interest rate
mInterest = calcMInterest( interest );
// call method to calculate and return the monthly payment
mPayment = calcMPayment (mInterest, principle, time);
// call method to print loan statement
} // end main ()
/******************************************************************************/
// this class calculates and returns the monthly interest on the loan
public static double calcMInterest( double interest )
{
double mInterest;
mInterest = (interest / 12);
return mInterest;
} // end calcMInterest
/******************************************************************************/
// this class calculates and returns the monthly payment
public static double calcMPayment (double mInterest, double principle, int time)
{
double mPayment;
mPayment = (mInterest * principle) / (1-(1+ Math.pow(mInterest,-time)));
return mPayment;
} // end calcMPayment
/******************************************************************************/
// this class prints a loan statement showing the amount borrowed
// and the amount borrowed, the annual interest rate, the number of months
// and the monthly payment
public static void loanStatement(double principle, double interest, int time, double mPayment)
{
System.out.println(" principle is" + principle);