You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取最高薪资PartTimeEmployee姓名的方法返回结果异常问题

问题:获取薪资最高的兼职员工姓名失败,返回了第一个员工名字

我需要实现一个算法,返回薪资最高的PartTimeEmployee姓名,预期结果是"Wills",但当前方法总是返回数组第一个位置的名字"Muwana"。

当前方法代码

public string PartTimeEmployeeWithHighestSalary()
{
    string name = employees[0].GetName();
    double salary = employees[0].GetSalary();

    for (int i = 0; i < count; i++)
    {
        if (employees[i].GetSalary() > salary && employees[i] is PartTimeEmployee)
        {
            salary = employees[i].GetSalary();
            name = employees[i].GetName();
        }
    }
    return name;
}

程序测试代码

static void Main(string[] args)
{
    Employee EmpS1 = new ServiceEmployee("S123", "Muwana", "Sein",1000);
    Employee EmpS2 = new ServiceEmployee("S124", "Prince", "Jack",1500);
    Employee EmpS3 = new ServiceEmployee("S125", "Matengu", "Prince",6000);
    Employee EmpP1 = new PartTimeEmployee("P133", "Mad", "Max",2500);
    Employee EmpP2 = new PartTimeEmployee("P134", "Wills", "Sanjay",3000);
    Employee EmpP3 = new PartTimeEmployee("P135", "Rick", "Sanchez",1000);
    Employee EmpM1 = new ManagementEmployee("M143", "Morty", "Smith",1500);
    Employee EmpM2 = new ManagementEmployee("M143", "Summer", "Smith",6500);
    Employee EmpM3 = new ManagementEmployee("M144", "Beth", "Smith",6500);

    HRDepartment company = new HRDepartment(500);

    company.AddEmployee(EmpS1);
    company.AddEmployee(EmpS2);
    company.AddEmployee(EmpS3);
    company.AddEmployee(EmpP1);
    company.AddEmployee(EmpP2);
    company.AddEmployee(EmpP3);
    company.AddEmployee(EmpM1);
    company.AddEmployee(EmpM2);
    company.AddEmployee(EmpM3);

    //-1. PartTime employee with highest salary
    Console.WriteLine("PartTime employee with highest salary:{0}",company.PartTimeEmployeeWithHighestSalary());
}

错误原因

当前代码的核心问题是初始值没有限定为PartTimeEmployee类型:

  • 直接用数组第一个非兼职员工(ServiceEmployee类型的Muwana)初始化name和salary,初始值本身不符合筛选条件;
  • 若后续兼职员工薪资未超过初始值,会错误返回非兼职员工的名字;即使有更高薪资的兼职员工,也可能因count未正确统计员工数量等隐性问题导致遍历不完整。

修复方案

方案一:初始化最低薪资,遍历筛选兼职员工

将初始薪资设为double.MinValue,遍历所有员工时先判断是否为兼职员工,再比较薪资更新结果:

public string PartTimeEmployeeWithHighestSalary()
{
    string name = null;
    double highestSalary = double.MinValue;

    for (int i = 0; i < count; i++)
    {
        // 先判断类型,并用模式匹配直接转换为PartTimeEmployee
        if (employees[i] is PartTimeEmployee partTimeEmp)
        {
            double currentSalary = partTimeEmp.GetSalary();
            if (currentSalary > highestSalary)
            {
                highestSalary = currentSalary;
                name = partTimeEmp.GetName();
            }
        }
    }
    // 可根据需求处理无兼职员工的情况,比如返回提示文本
    return name ?? "No PartTime Employees";
}

方案二:先找到第一个兼职员工作为初始值

先定位第一个兼职员工,以此为初始值再继续比较后续的兼职员工,避免初始值无效:

public string PartTimeEmployeeWithHighestSalary()
{
    // 先定位第一个兼职员工
    int startIndex = 0;
    while (startIndex < count && !(employees[startIndex] is PartTimeEmployee))
    {
        startIndex++;
    }

    // 没有找到兼职员工的情况
    if (startIndex >= count)
    {
        return "No PartTime Employees";
    }

    string name = employees[startIndex].GetName();
    double highestSalary = employees[startIndex].GetSalary();

    // 从第一个兼职员工的下一位开始遍历
    for (int i = startIndex + 1; i < count; i++)
    {
        if (employees[i] is PartTimeEmployee partTimeEmp)
        {
            double currentSalary = partTimeEmp.GetSalary();
            if (currentSalary > highestSalary)
            {
                highestSalary = currentSalary;
                name = partTimeEmp.GetName();
            }
        }
    }
    return name;
}

内容的提问来源于stack exchange,提问作者Prince Sein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 06:31:21