Java 调用一个只打印一个";贷款结单“;

Java 调用一个只打印一个";贷款结单“;,java,Java,我正在做一个课堂作业,我们刚刚开始制定自己的方法,我觉得很简单的事情变得非常沮丧,希望你们能帮我解决这个问题 首先,我要完成的任务是:制作一个模块化的程序来计算每月付款,这似乎很容易,但这个问题上的一些限制如下 主要方法应: 向用户询问 贷款金额 年利率(小数点后7.5%为0.075) 月数 及 调用方法计算并返回月利率(年利率/12) 调用一个方法来计算并返回每月付款 调用一个方法来打印贷款对账单,显示借款金额、年利率、月数和每月付款 我已经快打印完贷款对账单了,但我一生都无法找到正确

我正在做一个课堂作业,我们刚刚开始制定自己的方法,我觉得很简单的事情变得非常沮丧,希望你们能帮我解决这个问题

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

主要方法应:

向用户询问

  • 贷款金额
  • 年利率(小数点后7.5%为0.075)
  • 月数

  • 调用方法计算并返回月利率(年利率/12)
  • 调用一个方法来计算并返回每月付款
  • 调用一个方法来打印贷款对账单,显示借款金额、年利率、月数和每月付款
我已经快打印完贷款对账单了,但我一生都无法找到正确的方式来调用它,并且在我运行程序时让它显示出来:/因此,如果您能帮助我了解它是如何完成的,我将不胜感激

(我意识到我的代码中可能还有其他错误,但现在我宁愿只关注我需要完成的事情) 导入java.util.Scanner; 公共类贷款支付{

/**
 * 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);

如果您只需执行打印贷款对账单的调用方法,则下面一行需要执行以下操作:

loanStatement(principle, interest, time, mPayment);
它应该很好用

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

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

检查此解决方案;)


好的……我发布了一个答案,然后删除了它,因为你实际上没有在这里使用多个类。你能澄清一下你的程序到底在做什么,以及你期望它做什么吗?@nhgrif这应该是创建方法的练习。我需要获得用户输入的贷款信息,然后制作一个计算月利息的方法,另一种计算每月付款的方法,最后一种方法需要打印包含所有信息的贷款对账单看起来您已经有了正确调用的计算利息和付款的方法(
calcMPayment()
)。您应该能够调用
loanStatement()
method以同样的方式,只需将你读到的所有论点都传递进去。请看我贴出的答案。如果你还有问题,你需要澄清你到底在哪里有问题。先生,你真是太棒了!我知道这很简单,但就是拿不到它><我将浏览我的文章,看看是否有提到类似的东西,但我没有是的,我最初把它设为一个字符串,让它返回值,这样main方法就可以把它打印出来(就像我文本中所有的例子一样)但是作业的措辞清楚地说明了调用实际打印的方法,所以我不能:/@JeffreyQuinn是的,我理解。作业就是作业。我答案的最后一段更值得思考。当我交作业并有这样的想法时,我通常会提供两种解决方案(其中一个完全被注释掉)把老师的注意力转移到我觉得更好的替代方法上。
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);
    }
}