C#控制台员工管理程序List无法保存用户输入数据问题
问题原因
你的员工数据无法留存的核心问题是变量作用域生命周期错误,附带还有3个影响功能使用的小问题:
- 核心bug:
List<Employee> EmployeesList = new List<Employee>();被写在了while (keepRunning)循环的内部。C#中定义在大括号{}代码块内的局部变量,仅在当前次代码块执行时有效,每轮循环结束后,上一轮创建的列表就会被系统回收,下一轮循环启动时会重新创建一个全新的空列表,之前存入的员工数据自然全部丢失。 - 功能入口缺失:你写的遍历打印全量员工的逻辑绑定在
case 0分支,但菜单提示里只列了1/2/3三个选项,用户完全不知道按0可以查看员工列表。 - 删除逻辑提示错误:当找不到对应员工时,
employeeToBeDeleted是null值,此时输出$"Could not find {employeeToBeDeleted}"只会打印空内容,不会显示用户输入的待删除姓氏,提示无意义。 - 冗余代码:你定义了本地函数
AddToList但全程没有调用,属于无效代码。
修复步骤
- 把员工列表的初始化代码挪到
while循环外部,放在Main方法刚进入、循环启动前的位置,保证整个程序运行期间列表只创建一次,不会被反复重置。 - 调整菜单提示,增加“按0查看所有员工”的说明,也可以根据需求把查看员工的逻辑整合到其他分支(比如加完员工、删完员工自动打印当前列表)。
- 修复删除失败的提示文本,直接输出用户输入的待删除姓氏即可。
- 删掉没用到的
AddToList冗余函数。
修正后的核心代码结构
using Employees; namespace Inheritance { public class Program { static void Main(string[] args) { bool keepRunning = true; // 列表挪到循环外,整个程序运行周期只初始化一次 List<Employee> EmployeesList = new List<Employee>(); while (keepRunning) { // 菜单提示补上0选项 Console.WriteLine("TIFFANYS & CO - PRIVATE PROGRAM TM\nPress the number of the action you want to pursue, and press [ENTER].\n0) View all Employees\n1) Add an Employee\n2) Delete an Employee\n3) Exit"); var menuChoice = Int32.Parse(Console.ReadLine()); switch (menuChoice) { case 0: foreach (Employee item in EmployeesList) { Console.WriteLine(item.firstName + " " + item.lastName + " " + item.salary); } break; case 1: Console.WriteLine("a) Add a Manager\nb) Add an Engineer\nc) Add a Researcher"); var menuChoice1 = Console.ReadLine(); switch (menuChoice1) { case "a": Console.WriteLine("Please type in the first name of the manager and press [ENTER]:"); string managerFirstName = Console.ReadLine(); Console.WriteLine("Please type in the last name of the manager and press [ENTER]:"); string managerLastName = Console.ReadLine(); Console.WriteLine("Please type in the salary of the manager and press [ENTER]:"); int managerSalary = Int32.Parse(Console.ReadLine()); Console.WriteLine("Please type in how many meetings the manager will attend weekly and press [ENTER]:"); int managerMeetings = Int32.Parse(Console.ReadLine()); Console.WriteLine("Please type in how many vacation-weeks the manager has annually and press [ENTER]:"); int managerVacationWeeks = Int32.Parse(Console.ReadLine()); Manager manager = new(managerFirstName, managerLastName, managerSalary, managerMeetings, managerVacationWeeks); EmployeesList.Add(manager); foreach (var employee in EmployeesList) { Console.WriteLine(employee); } break; case "b": Console.WriteLine("Please type in the first name of the engineer and press [ENTER]:"); string engineerFirstName = Console.ReadLine(); Console.WriteLine("Please type in the last name of the engineer and press [ENTER]:"); string engineerLastName = Console.ReadLine(); Console.WriteLine("Please type in the salary of the engineer and press [ENTER]:"); int engineerSalary = Int32.Parse(Console.ReadLine()); Console.WriteLine("How many years of experience does the engineer have? Type the number of years and press [ENTER]:"); int engineerExperience = Int32.Parse(Console.ReadLine()); Console.WriteLine("What kind of engineering is the engineers field? Please write their field and press [ENTER]:"); string engineerField = Console.ReadLine(); Engineer engineer = new(engineerFirstName, engineerLastName, engineerSalary, engineerExperience, engineerField); EmployeesList.Add(engineer); break; case "c": Console.WriteLine("Please type in the first name of the researcher and press [ENTER]:"); string researcherFirstName = Console.ReadLine(); Console.WriteLine("Please type in the last name of the researcher and press [ENTER]:"); string researcherLastName = Console.ReadLine(); Console.WriteLine("Please type in the salary of the researcher and press [ENTER]:"); int researcherSalary = Int32.Parse(Console.ReadLine()); Console.WriteLine("What university did the researcher get their Doctorate? Please type in the name of the university and press [ENTER]:"); string researcherUniversity = Console.ReadLine(); Console.WriteLine("What was the thesis statement of the researchers doctorate? Please type in the thesis question and press [ENTER]:"); string researcherThesis = Console.ReadLine(); Researcher researcher = new(researcherFirstName, researcherLastName, researcherSalary, researcherUniversity, researcherThesis); EmployeesList.Add(researcher); break; } break; case 2: Console.WriteLine("What is the last name of the employee to be deleted?"); string toDeleteLastName = Console.ReadLine(); Employee employeeToBeDeleted = EmployeesList.Find(e => e.lastName == toDeleteLastName); if (employeeToBeDeleted != null) EmployeesList.Remove(employeeToBeDeleted); else // 修复提示内容,直接输出用户输入的姓氏 Console.WriteLine($"Could not find employee with last name: {toDeleteLastName}"); break; case 3: keepRunning = false; break; } } } } }
初学者提示:写循环逻辑的时候要特别注意变量定义的位置,如果你需要在多轮循环中共享同一个变量的数据,就一定要把变量定义在循环体外面;如果每轮循环都需要全新的初始值,再把变量定义在循环内部。
内容的提问来源于stack exchange,提问作者ayoubzann
相关产品推荐
相关产品推荐

