未返回正确值的静态类方法



该程序的理念是输出该部门的每个工作人员加起来工资最高的部门。所以我有我的程序.cs:

string print = string.Empty;
int n = int.Parse(Console.ReadLine());
for(int a = 0; a < n; a++)
{
string input = Console.ReadLine();
List<string> inputs = input.Split(" ").ToList();
if(inputs[4].Contains("@"))
{
Employee info = new Employee(inputs[0], double.Parse(inputs[1]), inputs[2], inputs[3], inputs[4], int.Parse(inputs[5]));
print = info.ToString();
}
else
{
Employee info = new Employee(inputs[0], double.Parse(inputs[1]), inputs[2], inputs[3], "n/a", int.Parse(inputs[4]));
print = info.ToString();
}
Employee.Calculation(inputs[3], double.Parse(inputs[1]));
}
Console.WriteLine(print);

以及我的Employee.cs的一部分,这是最重要的一部分:

public static void Calculation(string department, double salary)
{
Dictionary<string, double> data = new Dictionary<string, double>();
if (data.ContainsKey(department))
{
data[department] += salary;
}
else
{
data.Add(department, salary);
}
foreach (KeyValuePair<string, double> info in data)
{
if (info.Value > biggestSalary)
{
biggestSalary = info.Value;
toReturn = info.Key;
}
}
}
public override string ToString()
{
string line1 = "Highest average salary: " + toReturn;
return line1;

}

使用此输入:

4
Pesho 120000 Dev Daskalo pesho@abv.bg 28
Toncho 333333.33 Manager Marketing 33
Ivan 15000 ProjectLeader Development ivan@ivan.com 40
Gosho 130033333 Freeloader Nowhere 18

当我调试它时,由于某种原因,最后一行被忽略了,它返回了部门中第二大工资";市场营销";。使用此输入:

6
Stanimir 496.37 Temp Coding stancho@yahoo.com 50
Yovcho 610.13 Manager Sales 33
Toshko 609.99 Manager Sales toshko@abv.bg 44
Venci 0.02 Director BeerDrinking beer@beer.br 23
Andrei 700.00 Director Coding 45
Popeye 13.3333 Sailor SpinachGroup popeye@pop.ey 67

我得到";编码";而不是";销售";。当你把两个人结合在一起工作时;编码";你得到700+496=1196。当你把在";销售;则得到609+610=1219,然后输出应该是"0";最高平均工资:销售";,而是输出为"0";最高平均工资:编码";;

每次调用Calculation方法时都会创建一个新字典。

Dictionary<string, double> data = new Dictionary<string, double>();

// The first block is never called as the Dictionary never contains anything at this point. The else block always runs.    
if (data.ContainsKey(department))
{
data[department] += salary;
}
else
{
data.Add(department, salary);
}

因此,对于要添加的一名员工,字典中只有一个值。

由于编码部门的员工具有最高的个人价值,即返回的部门。

在不对代码的其他方面进行评论的情况下,避免此问题的方法是首先在Calculation方法之外创建Dictionary。

假设您将Employee添加到List<Employee>中,使用LINQ查找该值看起来像

public class Employee
{
public string Name { get; set; }
public string Dept { get; set; }
public decimal Salary { get; set; }
}
// preload
var empList = new List<Employee>(); 
empList.Add(new Employee(){Name = "A", Salary = 10, Dept = "Sales" });
empList.Add(new Employee(){Name = "B", Salary = 10, Dept = "Coding" });
empList.Add(new Employee(){Name = "C", Salary = 30, Dept = "Sales" });
empList.Add(new Employee(){Name = "D", Salary = 20, Dept = "Coding" });

// execute
var topItem =  empList.GroupBy(_ => _.Dept)
.Select(g => new { D = g.First().Dept, TS = g.Sum(s => s.Salary)})
.OrderByDescending(item => item.TS)
.First();
Console.WriteLine($"The department '{topItem.D}' has biggest salary: '{topItem.TS}'");

"销售"部门的工资最高:"40">

返回到您的代码结构在您的问题中起作用的观点。即使你在循环中计算这个,你仍然希望在类/应用程序范围变量上积累你的员工,这样你就可以连续访问它

适用于您的案例,您的Employee info = new Employee似乎没有添加到任何列表中。并且Employee.Calculation不使用任何程序杠杆变量,这将保持状态。

如果我想保留这个结构,你所拥有的结构,我可以像一样声明你的类

public class Employee
{
private static List<Employee> _empList = new List<Employee>(); 
// your constructor
public Employee (........)
{
// Assign your properties here
_empList.Add(this);
}

// And your `Employee.Calculation` would lose any parameters and look like this 
public static void Calculation()
{
var topItem =  _empList.GroupBy(_ => _.Dept).......

Console.WriteLine($"The department '{topItem.D}' has biggest salary: '{topItem.TS}'");
}
}

^^^如果我真的想解决这个问题,但保留你已经拥有的结构

最新更新