Java 贷款支付计算器,如何实现多种方法。无法开发与主要方法交互的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19215592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loan payment calculator, how to implement multiple methods. Trouble developing method to interact with main method
提问by user250557
The idea of this assignment is to have multiple methods interact with each other. I am asking the user for loan amount, interest rate and duration of loan. Then the program is supposed to have one method that calculates the monthly rate, one method that calculates and returns the monthly payment and a method to print the loan statement (amt borrowed,annual interest rate, number of months, and the monthly payment).
这个分配的想法是让多个方法相互交互。我向用户询问贷款金额、利率和贷款期限。然后该程序应该有一种计算月利率的方法,一种计算和返回月付款的方法以及一种打印贷款报表的方法(借入金额、年利率、月数和月付款)。
I am not receiving any errors in the editor but my program just asks for the three inputs from the user and does not print the loan statement. Any suggestions?
我在编辑器中没有收到任何错误,但我的程序只是要求用户提供三个输入,并且不打印贷款声明。有什么建议?
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
I am not sure if some of my code is still redundant.
我不确定我的某些代码是否仍然是多余的。
回答by Juned Ahsan
Your method CalcMonthlyInterestRate
need to be within your CarLoan
class and not outside of it.
你的方法CalcMonthlyInterestRate
需要在你的CarLoan
班级之内,而不是在它之外。
回答by Tdorno
Since the main method is static and your CalcMonthlyInterestRate references your main method the CalcMonthlyInterestRate must also be static so that the two create a static reference
to each other.
由于主要方法是静态的并且您的 CalcMonthlyInterestRate 引用您的主要方法,因此 CalcMonthlyInterestRate 也必须是静态的,以便两者相互创建static reference
。
At the bottom of your post we see:
在您的帖子底部,我们看到:
}//end main
}//end class
Class Methods referenced by the main method must also be inside its same class as well as being static
. Once you start building your own classes and objects this won't always be the case
由 main 方法引用的类方法也必须在它的同一个类中,并且是static
. 一旦您开始构建自己的类和对象,情况就不会总是如此
}//end main
public static double CalcMonthlyInterestRate(double annualInterestRate) {
double monthlyInterestRate;
monthlyInterestRate = (annualInterestRate/12);
return monthlyInterestRate;
}
}//end class
To capture a double using your method just call something like this in your main method:
要使用您的方法捕获双精度值,只需在您的主方法中调用类似的东西:
double answer = CalcMonthlyInterestRate(/*some double variable here*/); //in main
回答by Millie Smith
That's because you have it like this:
那是因为你有这样的:
import java.util.Scanner;//instance of scanner class for input from user
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
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();
}
}//end main method
public static double CalcMonthlyInterestRate(double annualInterestRate)
{
double monthlyInterestRate;
monthlyInterestRate = (annualInterestRate/12);
return monthlyInterestRate;
}
There are a couple problems here... One is that //end main methodis not really the end of the main method. It's the end of your class. All methods in Java have to be inside a class. Classes represent objects, and Java is "object-oriented". Each method on an object represents a "behavior" of that object. A behavior dangling applied to nothing is (most of the time) meaningless.
这里有几个问题......一个是//end main 方法并不是真正的 main 方法的结束。这是你的课结束了。Java 中的所有方法都必须在一个类中。类代表对象,而 Java 是“面向对象的”。对象上的每个方法都代表该对象的“行为”。悬空应用于任何事物的行为(大部分时间)是毫无意义的。
The "class, interface, or enum" expected means that where you are currently typing code, you can only put a class, an interface, or an enum. Those are the top level constructs of a Java program (unless you want to get technical with packages, etc...)
预期的“类、接口或枚举”意味着您当前正在输入代码的位置只能放置类、接口或枚举。这些是 Java 程序的顶级构造(除非您想获得软件包等方面的技术......)
Putting the function inside the class (and fixing some code redundancy), we have:
将函数放在类中(并修复一些代码冗余),我们有:
import java.util.Scanner;//instance of scanner class for input from user
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
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();
double monthlyInterestRate = CalcMonthlyInterestRate(annualInterestRate);
System.out.println("Please enter the number of months you have to pay back your loan.");
numberOfMonths = keyboard.nextInt();
} //end main method
public static double CalcMonthlyInterestRate(double annualInterestRate)
{
return annualInterestRate / 12;
} //end monthly interest method
} //end class
Also, methods in Java usually start with a lowercase letter.
此外,Java 中的方法通常以小写字母开头。