调用只打印出“贷款声明”的方法

时间:2013-10-07 00:41:55

标签: java

我正在为课堂做作业,我们刚刚开始制作自己的方法,我认为看起来很容易变得非常沮丧,并希望你能帮助我绕过它。

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

主要方法应该:

要求用户

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

2 个答案:

答案 0 :(得分:0)

如果你还剩下// call method to print loan statement,那么这就是你在下面一行所需要的:

loanStatement(principle, interest, time, mPayment);

它应该可以正常工作。

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

但是,我个人的偏好是将loanStatement更改为String返回类型,并将打印语句放在main中并打印loanStatement的返回值。返回Strings的方法几乎一样容易,并且更灵活,以备将来使用(例如,如果您希望允许程序也写入文件,则需要两个loanStatement方法,或者完全返工{ {1}})。

答案 1 :(得分: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);
    }
}