-1

我在使用员工年龄的值时遇到问题,并且我有 primeagechecker。员工具有年龄本身的价值。Primeagechecker 只检查它是否是素数。然后只需打印一些命令“年龄是素数”。如果它是素数。我不是 Java 人,我是 Java 的初学者。年龄检查的结果总是错误的。感谢您的帮助。

这是我的代码。

public class Employee {
    String name;
    boolean check;
    int age;
    Department department;
    public ArrayList<Employee> emplo;

    static Employee emp1 = new Employee(Department.Accounting,"Counting Guru",55);
    static Employee emp2 = new Employee(Department.Accounting,"Counting Pro", 45);
    static Employee emp3 = new Employee(Department.Accounting,"Counting Savvy", 40);
    static Employee emp4 = new Employee(Department.Accounting,"Counting Novice", 25);
    static Employee emp5 = new Employee(Department.Marketing,"Sales Guru", 50);
    static Employee emp6 = new Employee(Department.Marketing,"Sales Pro", 48);
    static Employee emp7 = new Employee(Department.Marketing,"Sales Savvy", 38);
    static Employee emp8 = new Employee(Department.Human_Resources,"Hiring Guru", 58);
    static Employee emp9 = new Employee(Department.Human_Resources,"Hiring Pro", 47);
    static Employee emp10 = new Employee(Department.Information_Systems,"Hacking Pro", 46);
    static Employee emp11 = new Employee(Department.Information_Systems,"Hacking Guru", 51);
    static Employee emp12 = new Employee(Department.Information_Systems,"Hacking Savvy", 38);
    static Employee emp13 = new Employee(Department.Information_Systems,"Hacking Novice", 23);

    Employee(Department department,String name, int age)
    {
        this.department = department;
        this.name = name;
        this.age = age;
    }

    public int getAge()
    {
        return age;
    }

    public String getName()
    {
        return name;
    }

    public boolean GetChecker()
    {
        return check;

    }

    public void addEmplo(Employee x){
        if (emplo.isEmpty())
        {
            emplo.add(x);
        }
        else
        {
            int i;
            for ( i = 0;i <emplo.size(); ++i){
                if(emplo.get(i).getAge() > x.getAge()){
                    emplo.add(i,x);
                    break;
                }
            }

            if ( i == emplo.size()){
                emplo.add(x);
            }
        }
        }

    public ArrayList<Employee> getEmplo(){
        return emplo;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(getDept(department));
        sb.append("\t");
        sb.append(getName());
        sb.append("\t");
        sb.append(getAge());
        sb.append("\t");
        sb.append(GetChecker());

        return sb.toString();
    }

    private Department getDept(Department department){
        return department;
    }

}





public class PrimeAgeChecker{

    Employee age;

    PrimeAgeChecker(Employee age)
    {
        this.age = age;
    }

    public boolean check(){

        boolean status = false;

        for (int a = 2; a < age; ++a){
            if (age % a == 0)
            {
                status = true;
            }
        }

        return status;

    }
}
4

2 回答 2

0

假设你没有遗漏任何东西,你永远不会调用方法check()checkcheck(). 实际上,您没有PrimeAgeChecker.

添加static修饰符PrimeAgeChecker并调用PrimeAgeChecker.check(),或执行类似的操作PrimeAgeChecker pac=new PrimeAgeChecker();pac.check();

于 2013-10-24T00:54:47.340 回答
0

在您的黄金年龄检查器中,您的年龄属于 Employee 类型,这不是某种类型的数字。我什至不确定a < age应该编译。

无论发生什么,您都试图查看 Employee 是否可以被 int 整除,但这是不会发生的。您在 PrimeAgeChecker 中的年龄应定义为 int 并使用 employee.getAge() 设置

于 2013-10-24T00:33:14.237 回答