java 方法不适用于参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15286774/
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
Method not applicable for arguments
提问by Larry Gyamfi
I'm trying to create a tester class, but in doing so I have gotten an error which states:
我正在尝试创建一个测试员类,但这样做时我收到了一个错误,指出:
1 error found:
Error: The method printData(double, double) in the type PayCalculator is not
applicable for the arguments ()
How do I fix it? This my tester code
我如何解决它?这是我的测试员代码
PayCalculator p1 = new PayCalculator();
p1.setHourlyRate(8.25);
p1.setHoursWorked(45.0);
p1.printData();
my main code
我的主要代码
{
private double hourlyRate;
private double hoursWorked;
public PayCalculator()
{
hourlyRate = 0.0;
hoursWorked = 0.0;
}
/**
* Two parameter constructor
* Add hourlyRate and hoursWorked
* @param the hourly rate
* @param the hours worked
*/
public PayCalculator(double aHourlyRate, double aHoursWorked)
{
hourlyRate = aHourlyRate;
hoursWorked = aHoursWorked;
}
/**
* sets the hourly rate
* @return hourlyRate
*/
public void setHourlyRate(double aHourlyRate)
{
hourlyRate = aHourlyRate;
}
/**
* gets the hourly rate
* @param hourlyRate
*/
public double getHourlyRate()
{
return hourlyRate;
}
/**
* sets the hours worked
* @return hoursWorked
*/
public void setHoursWorked(double aHoursWorked)
{
hoursWorked = aHoursWorked;
}
/**
* gets the hours worked
* @param hours worked
*/
public double getHoursWorked()
{
return hoursWorked;
}
public boolean workedOvertime()
{
if (hoursWorked > 40)
{
return true;
}
else
{
return false;
}
}
public double numHoursOvertime()
{
if (hoursWorked > 40)
{
return hoursWorked - 40;
}
else
{
return 0;
}
}
public double calculateGrossPay()
{
if (hourlyRate <= 10.25)
{
if (hourlyRate <= 40)
return hourlyRate * hoursWorked;
}
else
{
double grossPay = ((40 * hourlyRate) + ((hourlyRate * 2) * hoursWorked - 40));
return grossPay;
}
if (hoursWorked <= 60)
{
return hourlyRate * hoursWorked;
}
else
{
return 60 * hourlyRate;
}
}
public double determineTaxRate(double grossPay)
{
if (grossPay >= 800)
{
double tax = 0;
tax = grossPay * 0.37;
return tax;
}
else if ( grossPay >= 400)
{
double tax = 0;
tax = grossPay * 0.22;
return tax;
}
else
{
double tax = 0;
tax = grossPay * 0.12;
return tax;
}
}
public double calculateNetPay(double grossPay, double tax)
{
double calculateNetPay = grossPay - tax;
return calculateNetPay;
}
public void printData(double grossPay, double tax)
{
System.out.println("Hours Worked: " + hoursWorked);
System.out.println("Hourly rate: " + hourlyRate);
System.out.println("Number of hours of overtime worked: " + numHoursOvertime());
System.out.println("Worked overtime? " + workedOvertime());
System.out.println("Gross pay: " + calculateGrossPay());
System.out.println("Tax Rate: " + determineTaxRate(grossPay));
System.out.println("Net Pay: " + calculateNetPay(grossPay, tax));
}
}
回答by Jon Skeet
Well yes. Here's the method declaration:
嗯,是。这是方法声明:
public void printData(double grossPay, double tax)
And here's how you're trying to call it:
这是您尝试调用它的方式:
p1.printData();
What values do you expect to be used for grossPay
and tax
?
您希望将哪些值用于grossPay
和tax
?
To be honest it seems odd that you should have to pass in grossPay
having already specified the hours worked and hourly rate, but that's a different matter. The most immediate problem is that you're not providing the arguments your method requires.
老实说,您必须grossPay
已经指定工作时间和小时费率,这似乎很奇怪,但这是另一回事。最直接的问题是您没有提供方法所需的参数。
回答by Abubakkar
Your method expects two values grossPay
and tax
:
您的方法需要两个值grossPay
和tax
:
public void printData(double grossPay, double tax)
But you are not providing grossPay
and tax
to it when calling it :
但是在调用它时你没有提供 grossPay
和tax
给它:
p1.printData();
Include the value of grossPay
and tax
when calling your method and it will work fine like :
包括价值 grossPay
和tax
调用您的方法时,它会像罚款:
p1.printData(123.00,2.5);
回答by navyad
Definition of your method is:
您的方法的定义是:
public void printData(double grossPay, double tax)
It expects call with two double arguments whereas you are calling with no argument:
它期望使用两个双参数调用,而您不带参数调用:
p1.printData();
Pass double arguments to printData(44.85d, 77.56d)
将双参数传递给 printData(44.85d, 77.56d)