好的,我浏览了这个网站上的很多论坛,但是找不到我的问题。我一直收到一条错误,上面写着“无法找到符号”并指向我的EmployeeTest应用中的“新”中的“n”。这是我的代码:
第一个文件:
import java.util.Scanner;
public class Employee
{
private String fName;
private String lName;
private Double mSalary;
public Employee( String first, String last, Double mth)
{
fName = first;
lName = last;
if ( mth > 0.00 )
mSalary = mth;
if ( mth < 0.00 )
mSalary = 0.00;
}
public void setFName( String first )
{
fName = first;
}
public void setLName( String last )
{
lName = last;
}
public void setMSalary( Double mth )
{
mSalary = mth;
}
public String getFName()
{
return fName;
}
public String getLName()
{
return lName;
}
public Double getMSalary()
{
return mSalary;
}
public void displayMessage()
{
System.out.printf( "%s %s has a monthly salary of $%.2f\n",
getFName(),
getLName(),
getMSalary() );
}
}
第二档:
public class EmployeeTest
{
public static void main( String[] args )
{
Employee myEmployee = new Employee(
"Fred", "Rogers", "10" );
System.out.printf( "Employee's first name is: %s\n",
myEmployee.getFName() );
System.out.printf( "\nEmployee's last name is: %s\n",
myEmployee.getLName() );
System.out.printf( "\nEmployee's monthly salary is: %d\n",
myEmployee.getMSalary() );
}
}
我觉得它与我的构造函数有关但我无法找出问题所在!我一定要把我的代码看了几千次!
答案 0 :(得分:3)
您有public Employee( String first, String last, Double mth)
作为构造函数,但是您使用Employee
实例化new Employee("Fred", "Rogers", "10");
对象
错误很可能是说找不到带(string, string, string)
个参数的构造函数。
将"10"
更改为10
:new Employee("Fred", "Rogers", 10);
答案 1 :(得分:3)
您班级中的构造函数是:
public Employee( String first, String last, Double mth)
但你正在打电话
Employee myEmployee = new Employee( "Fred", "Rogers", "10" );
更改构造函数以传递String
public Employee( String first, String last, String mth)
或将10.0
作为double值传递(这似乎是一个更好的解决方案)。
Employee myEmployee = new Employee( "Fred", "Rogers", 10.0d );
答案 2 :(得分:3)
更改EmployeeTest
Employee myEmployee = new Employee("Fred", "Rogers", "10" );
为:
Employee myEmployee = new Employee( "Fred", "Rogers", 10d );
和
System.out.printf( "\nEmployee's monthly salary is: %d\n", myEmployee.getMSalary() );
为:
System.out.printf( "\nEmployee's monthly salary is: %f\n", myEmployee.getMSalary() );