-1

I have been working on a program that is a Java loan payment calculator for my comp sci class. The programs requriements are:

  1. ask user for loan amount, annual interest rate, and number of months
  2. call a method to calculate and return the monthly interest rate
  3. call a method to calculate and return the monthly payments
  4. call a method to print a loan statement showing the amount borrowed, annual interest rate, the number of months and the monthly payment.

This is my first time working with multiple methods. I have developed the code so far as shown below. The netbeans editor is not giving me any errors, however, it does not print the statements from my last method. My questions are:

  1. are my methods set up correctly and passing parameters correctly
  2. why won't the statements print at the end
  3. am i capturing my other methods correctly? Do i need to enter variables in the main method and then call them in the final method.
  4. is any of my code redundant?

I am not really sure how to proceed from here.

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
4

2 回答 2

0

看看你的 main 方法,你会发现你所做的只是声明你的变量并接收输入。执行计算的其他方法和打印结果的loanStatment (sic) 方法永远不会被调用。

我建议您阅读基础知识,因为像这样的非常简单的错误会浪费更少的时间。

于 2013-10-07T02:38:44.970 回答
0

您需要更改您的主要方法,如下所示。

import java.util.Scanner;

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();

    System.out.println("Please enter your monthly payment.");
    monthlyPayment = keyboard.nextDouble();

    System.out.println("Your monthly interest rate is ");
    System.out.println(calcMonthlyInterestRate(annualInterestRate));

    System.out.println("Your monthly payment is ");
    System.out.println(calcMonthlyPayment(calcMonthlyInterestRate(annualInterestRate),loanAmount,numberOfMonths));

    loanStatment(loanAmount,annualInterestRate,numberOfMonths,monthlyPayment);


}


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,
                                                 int numberOfMonths, 
                                                 double monthlyPayment){
             System.out.println("Your loan amount is " +loanAmount);
             System.out.println(annualInterestRate);
             System.out.println(numberOfMonths);
             System.out.println(monthlyPayment);
  }


 }//end of CarLoan

我真的建议您阅读一些关于 main 方法在 Java 中的工作原理。这会很有帮助。

于 2013-10-07T06:21:17.970 回答