0

好的,我浏览了这个网站上的很多论坛,我找不到我的问题。我不断收到一条错误消息,指出“找不到符号”,并在我的 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() );
        }
}

我感觉它与我的构造函数有关,但我无法找出问题所在!我一定已经检查了我的代码 ka-jillion 倍!

4

3 回答 3

3

你有public Employee( String first, String last, Double mth)作为你的构造函数,但你正在实例化一个Employee对象new Employee("Fred", "Rogers", "10");

该错误很可能是说它找不到带参数的构造(string, string, string)函数。

更改"10"10new Employee("Fred", "Rogers", 10);

于 2012-09-02T03:05:51.410 回答
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作为双精度值传递(这似乎是一个更好的解决方案)。

Employee myEmployee = new Employee( "Fred", "Rogers", 10.0d );
于 2012-09-02T03:06:41.343 回答
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() );
于 2012-09-02T03:07:37.227 回答