当前位置: 首页 > 知识库问答 >
问题:

在不使用这么多条件语句的情况下从这个程序中找到一个员工,有什么帮助吗?这是一个学校项目,我必须使用结构

柴凌
2023-03-14

我已经为我的学校项目程序编写了这段代码(它仍然不完整),但它完成了大部分工作。问题是,我们必须使用结构来制作一个预定义雇员的程序,然后我们必须制作它,以便我们可以使用任何属性(姓名、雇员ID、血型、年龄等)来搜索所有的雇员。我找到了一种使用条件语句的方法(到目前为止,它适用于名称和雇员ID)。我已经意识到代码太长了,可能会有另一种方法来搜索程序。我还想让它显示所有男性/女性员工时,我们使用性别作为搜索属性。下面是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Terminals
{
    class Program


    {
        struct Employee
        {
            public string Name;
            public int EmployeeID;
            public string BloodGroup;
            public int Salary;
            public int Age;
            public string Gender;
        }
        static void Main(string[] args)
        {
            Employee employee1;
            Employee employee2;
            Employee employee3;
            Employee employee4;
            Employee employee5;
            Employee employee6;

            employee1.Name = "James";
            employee1.EmployeeID = int.Parse("10");
            employee1.BloodGroup = "B +ive";
            employee1.Salary = int.Parse("500000");
            employee1.Age = int.Parse("26");
            employee1.Gender = "Male";

            employee2.Name = "Ali Khan";
            employee2.EmployeeID = int.Parse("20");
            employee2.BloodGroup = "O +ive";
            employee2.Salary = int.Parse("250000");
            employee2.Age = int.Parse("22");
            employee2.Gender = "Male";

            employee3.Name = "Jessica Hills";
            employee3.EmployeeID = int.Parse("30");
            employee3.BloodGroup = "A -ive";
            employee3.Salary = int.Parse("400000");
            employee3.Age = int.Parse("25");
            employee3.Gender = "Female";

            employee4.Name = "William";
            employee4.EmployeeID = int.Parse("40");
            employee4.BloodGroup = "O -ive";
            employee4.Salary = int.Parse("700000");
            employee4.Age = int.Parse("29");
            employee4.Gender = "Male";

            employee5.Name = "Lizzy";
            employee5.EmployeeID = int.Parse("50");
            employee5.BloodGroup = "AB +ive";
            employee5.Salary = int.Parse("70000");
            employee5.Age = int.Parse("19");
            employee5.Gender = "Female";

            employee6.Name = "Kyle";
            employee6.EmployeeID = int.Parse("60");
            employee6.BloodGroup = "AB -ive";
            employee6.Salary = int.Parse("600000");
            employee6.Age = int.Parse("21");
            employee6.Gender = "Male";

            Console.WriteLine("Please enter the number of the term you would like to search with (1-6)?");
            Console.WriteLine("1. Name");
            Console.WriteLine("2. Employee ID");
            Console.WriteLine("3. Blood Group");
            Console.WriteLine("4. Salary");
            Console.WriteLine("5. Age");
            Console.WriteLine("6. Gender");
            Console.WriteLine("Please enter the number (1-6): ");
            int searchterm = int.Parse(Console.ReadLine());
            if (searchterm == 1)
            {
                Console.WriteLine("Please enter the name of the employee: ");
                string name = Console.ReadLine();
                if (name == employee1.Name)
                {
                    Console.Write("Name: " + employee1.Name);
                    Console.WriteLine("Employee ID: " + employee1.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee1.BloodGroup);
                    Console.WriteLine("Salary: " + employee1.Salary);
                    Console.WriteLine("Age: " + employee1.Age);
                    Console.WriteLine("Gender: " + employee1.Gender);
                    Console.ReadKey();
                }
                else if (name == employee2.Name)
                {
                    Console.Write("Name: " + employee2.Name);
                    Console.WriteLine("Employee ID: " + employee2.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee2.BloodGroup);
                    Console.WriteLine("Salary: " + employee2.Salary);
                    Console.WriteLine("Age: " + employee2.Age);
                    Console.WriteLine("Gender: " + employee2.Gender);
                    Console.ReadKey();
                }
                else if (name == employee3.Name)
                {
                    Console.Write("Name: " + employee3.Name);
                    Console.WriteLine("Employee ID: " + employee3.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee3.BloodGroup);
                    Console.WriteLine("Salary: " + employee3.Salary);
                    Console.WriteLine("Age: " + employee3.Age);
                    Console.WriteLine("Gender: " + employee3.Gender);
                    Console.ReadKey();
                }
                else if (name == employee4.Name)
                {
                    Console.Write("Name: " + employee4.Name);
                    Console.WriteLine("Employee ID: " + employee4.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee4.BloodGroup);
                    Console.WriteLine("Salary: " + employee4.Salary);
                    Console.WriteLine("Age: " + employee4.Age);
                    Console.WriteLine("Gender: " + employee4.Gender);
                    Console.ReadKey();
                }
                else if (name == employee5.Name)
                {
                    Console.Write("Name: " + employee5.Name);
                    Console.WriteLine("Employee ID: " + employee5.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee5.BloodGroup);
                    Console.WriteLine("Salary: " + employee5.Salary);
                    Console.WriteLine("Age: " + employee5.Age);
                    Console.WriteLine("Gender: " + employee5.Gender);
                    Console.ReadKey();
                }
                else if (name == employee6.Name)
                {
                    Console.Write("Name: " + employee1.Name);
                    Console.WriteLine("Employee ID: " + employee6.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee6.BloodGroup);
                    Console.WriteLine("Salary: " + employee6.Salary);
                    Console.WriteLine("Age: " + employee6.Age);
                    Console.WriteLine("Gender: " + employee6.Gender);
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Please try again.");
                    return;

                }
            }

            if (searchterm == 2)
            {
                Console.WriteLine("Please enter the employee ID of the employee");
                int employeeid = int.Parse(Console.ReadLine());
                if (employeeid == employee1.EmployeeID)
                {
                    Console.Write("Name: " + employee1.Name);
                    Console.WriteLine("Employee ID: " + employee1.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee1.BloodGroup);
                    Console.WriteLine("Salary: " + employee1.Salary);
                    Console.WriteLine("Age: " + employee1.Age);
                    Console.WriteLine("Gender: " + employee1.Gender);
                    Console.ReadKey();
                }
                else if (employeeid == employee2.EmployeeID)
                {
                    Console.Write("Name: " + employee2.Name);
                    Console.WriteLine("Employee ID: " + employee2.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee2.BloodGroup);
                    Console.WriteLine("Salary: " + employee2.Salary);
                    Console.WriteLine("Age: " + employee2.Age);
                    Console.WriteLine("Gender: " + employee2.Gender);
                    Console.ReadKey();
                }
                else if (employeeid == employee3.EmployeeID)
                {
                    Console.Write("Name: " + employee3.Name);
                    Console.WriteLine("Employee ID: " + employee3.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee3.BloodGroup);
                    Console.WriteLine("Salary: " + employee3.Salary);
                    Console.WriteLine("Age: " + employee3.Age);
                    Console.WriteLine("Gender: " + employee3.Gender);
                    Console.ReadKey();
                }
                else if (employeeid == employee4.EmployeeID)
                {
                    Console.Write("Name: " + employee4.Name);
                    Console.WriteLine("Employee ID: " + employee4.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee4.BloodGroup);
                    Console.WriteLine("Salary: " + employee4.Salary);
                    Console.WriteLine("Age: " + employee4.Age);
                    Console.WriteLine("Gender: " + employee4.Gender);
                    Console.ReadKey();
                }
                else if (employeeid == employee5.EmployeeID)
                {
                    Console.Write("Name: " + employee5.Name);
                    Console.WriteLine("Employee ID: " + employee5.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee5.BloodGroup);
                    Console.WriteLine("Salary: " + employee5.Salary);
                    Console.WriteLine("Age: " + employee5.Age);
                    Console.WriteLine("Gender: " + employee5.Gender);
                    Console.ReadKey();
                }
                else if (employeeid == employee6.EmployeeID)
                {
                    Console.Write("Name: " + employee6.Name +"   ");
                    Console.WriteLine("Employee ID: " + employee6.EmployeeID);
                    Console.WriteLine("Blood Group: " + employee6.BloodGroup);
                    Console.WriteLine("Salary: " + employee6.Salary);
                    Console.WriteLine("Age: " + employee6.Age);
                    Console.WriteLine("Gender: " + employee6.Gender);
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Please try again.");
                    Console.ReadKey();
                    return;
                    
                }




            }

        }

    }
}

共有1个答案

龙永思
2023-03-14

正如其他人所说的,LINQ将是您在这里的朋友,并且使用集合(如列表 )也会有极大的帮助。您可以消除else if逻辑,并大大缩短代码。

对于employee emp=employeeList.find(employee=>employee.name==name);行,您需要根据用户输入,将谓词更改为正确的结构成员find。如果它在列表中找不到employeeelse逻辑将这样报告(这也需要特定于每个搜索条件)。我还添加了一个私有方法来种子员工列表。一个包含所有控制台输出行的简单方法也会更加缩短这一点--只需将找到的employee提供给它。

唯一的问题是如果两个或多个雇员对他们的一个成员具有相同的值。除了gender之外,您的种子员工似乎没有任何共同之处,在这种情况下,您需要筛选一个附加属性以将其缩小到一个员工,或者根据搜索条件输出所有找到的员工。.find将返回与条件匹配的第一个员工,这样就不起作用了(或者至少只输出第一个找到的员工!)您可以将此代码更改为使用.findall,然后将新的列表 传递给私有方法,并循环该列表中的所有员工,输出他们的每个信息。重载我为获取列表 而编写的ValidateEmployee方法应该可以解决这个问题。您可以让所有条件执行.findall并删除接受单个employeevalidateemployee方法,由您决定!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Terminals
{
    class Program
    {
        struct Employee
        {
            public string Name;
            public int EmployeeID;
            public string BloodGroup;
            public int Salary;
            public int Age;
            public string Gender;
        }
        static void Main(string[] args)
        {
            List<Employee> employeeList = SeedEmployees();

            Console.WriteLine("Please enter the number of the term you would like to search with (1-6):");
            Console.WriteLine("1. Name");
            Console.WriteLine("2. Employee ID");
            Console.WriteLine("3. Blood Group");
            Console.WriteLine("4. Salary");
            Console.WriteLine("5. Age");
            Console.WriteLine("6. Gender");
            Console.WriteLine("Please enter the number (1-6): ");
            int searchterm = int.Parse(Console.ReadLine());

            if (searchterm == 1)
            {
                Console.WriteLine("Please enter the name of the employee: ");
                string name = Console.ReadLine();
                Employee emp = employeeList.Find(employee => employee.Name == name);

                ValidateEmployee(emp);
            }
            else if (searchterm == 6)
            {
                Console.WriteLine("Please enter the gender of the employee: ");
                string gender = Console.ReadLine();
                List<Employee> emps = employeeList.FindAll(employee => employee.Gender == gender);

                ValidateEmployee(emps);
            }

            //the rest of your "if" conditions for search terms...

            else
            {
                Console.WriteLine("Please try again.");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Done. Press any key to exit...");
            Console.ReadLine();
        }

        private static void ValidateEmployee(Employee emp)
        {
            if (emp.Equals(default(Employee)))
            {
                //employee not found
                Console.WriteLine($"Employee not found by supplied input. Please try again.");
                Console.ReadLine();
                return;
            }
            else
            {
                //output the emplyee info
                OutputEmployeeInfo(emp);
            }
        }

        private static void ValidateEmployee(List<Employee> emps)
        {
            foreach (Employee emp in emps)
            {
                if (emp.Equals(default(Employee)))
                {
                    //employee not found
                    Console.WriteLine($"Employee not found by supplied input.");
                }
                else
                {
                    //output the emplyee info
                    OutputEmployeeInfo(emp);
                }
            }
        }

        private static void OutputEmployeeInfo(Employee employee)
        {
            Console.Write("Name: " + employee.Name);
            Console.WriteLine("Employee ID: " + employee.EmployeeID);
            Console.WriteLine("Blood Group: " + employee.BloodGroup);
            Console.WriteLine("Salary: " + employee.Salary);
            Console.WriteLine("Age: " + employee.Age);
            Console.WriteLine("Gender: " + employee.Gender);
            Console.ReadKey();
        }

        private static List<Employee> SeedEmployees()
        {
            Employee employee1;
            Employee employee2;
            Employee employee3;
            Employee employee4;
            Employee employee5;
            Employee employee6;

            employee1.Name = "James";
            employee1.EmployeeID = int.Parse("10");
            employee1.BloodGroup = "B +ive";
            employee1.Salary = int.Parse("500000");
            employee1.Age = int.Parse("26");
            employee1.Gender = "Male";

            employee2.Name = "Ali Khan";
            employee2.EmployeeID = int.Parse("20");
            employee2.BloodGroup = "O +ive";
            employee2.Salary = int.Parse("250000");
            employee2.Age = int.Parse("22");
            employee2.Gender = "Male";

            employee3.Name = "Jessica Hills";
            employee3.EmployeeID = int.Parse("30");
            employee3.BloodGroup = "A -ive";
            employee3.Salary = int.Parse("400000");
            employee3.Age = int.Parse("25");
            employee3.Gender = "Female";

            employee4.Name = "William";
            employee4.EmployeeID = int.Parse("40");
            employee4.BloodGroup = "O -ive";
            employee4.Salary = int.Parse("700000");
            employee4.Age = int.Parse("29");
            employee4.Gender = "Male";

            employee5.Name = "Lizzy";
            employee5.EmployeeID = int.Parse("50");
            employee5.BloodGroup = "AB +ive";
            employee5.Salary = int.Parse("70000");
            employee5.Age = int.Parse("19");
            employee5.Gender = "Female";

            employee6.Name = "Kyle";
            employee6.EmployeeID = int.Parse("60");
            employee6.BloodGroup = "AB -ive";
            employee6.Salary = int.Parse("600000");
            employee6.Age = int.Parse("21");
            employee6.Gender = "Male";

            return new List<Employee>
            {
                employee1,
                employee2,
                employee3,
                employee4,
                employee5,
                employee6
            };
        }
    }
}
 类似资料: