-1

我正在做课堂作业,我们刚刚开始制定自己的方法,而我认为似乎很容易的事情变得非常沮丧,希望你能帮助我解决这个问题。

首先要做的事情是我要完成的任务是:制作一个模块化程序来计算每月付款,看起来很容易,但这个问题的一些限制如下

主要方法应该:

询问用户

  • 贷款金额
  • 年利率(以小数计,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);
4

2 回答 2

0

如果// call method to print loan statement这是您要做的所有事情,那么这就是您在它下面的行中需要的:

loanStatement(principle, interest, time, mPayment);

它应该可以正常工作。

您的其他方法具有非 void 返回类型,因此您someVariable = yourMethod(yourArguments)输入是为了接受返回值。但是,loanStatement有一个void返回类型。你不需要这样做。你可以像我上面展示的那样简单地调用它,它会执行方法中的代码。

不过,我个人的偏好是更改loanStatementString返回类型并将打印语句放入main并打印loanStatement. 几乎同样容易返回Strings并且更灵活以备将来使用的方法(例如,如果您想让程序也写入文件,则需要两种loanStatement方法,或者完全重做loanStatement)。

于 2013-10-07T00:44:15.783 回答
0

看看这个解决方案;)

public class LoanStatement{

    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
        loanStatement(principle,interest,time,mPayment);
    }

    // this method 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 method 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);
    }
}
于 2013-10-07T00:56:16.630 回答