我已经为我的学校项目程序编写了这段代码(它仍然不完整),但它完成了大部分工作。问题是,我们必须使用结构来制作一个预定义雇员的程序,然后我们必须制作它,以便我们可以使用任何属性(姓名、雇员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;
}
}
}
}
}
正如其他人所说的,LINQ将是您在这里的朋友,并且使用集合(如列表
)也会有极大的帮助。您可以消除else if
逻辑,并大大缩短代码。
对于employee emp=employeeList.find(employee=>employee.name==name);
行,您需要根据用户输入,将谓词更改为正确的结构成员find
。如果它在列表中找不到employee
,else
逻辑将这样报告(这也需要特定于每个搜索条件)。我还添加了一个私有方法来种子员工列表。一个包含所有控制台输出行的简单方法也会更加缩短这一点--只需将找到的employee
提供给它。
唯一的问题是如果两个或多个雇员对他们的一个成员具有相同的值。除了gender
之外,您的种子员工似乎没有任何共同之处,在这种情况下,您需要筛选一个附加属性以将其缩小到一个员工,或者根据搜索条件输出所有找到的员工。.find
将返回与条件匹配的第一个员工,这样就不起作用了(或者至少只输出第一个找到的员工!)您可以将此代码更改为使用.findall
,然后将新的列表
传递给私有方法,并循环该列表中的所有员工,输出他们的每个信息。重载我为获取列表
而编写的ValidateEmployee
方法应该可以解决这个问题。您可以让所有条件执行.findall
并删除接受单个employee
的validateemployee
方法,由您决定!
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
};
}
}
}
我必须创建程序,这样我就可以输入3个字母加上像“Jan1999”这样的一年,并显示日历。
如上图, 在 templates 文件夹中是html文件 在assembly文件夹中是.bat文件:
这可能是一个微不足道的问题,但是阅读ARG和ENV的文档并没有让我明白。 我正在构建一个PHP-FPM容器,我希望能够根据用户需要启用/禁用一些扩展。 如果可以在Dockerfile中通过在构建命令上添加条件和传递标志来实现这一点,那就太好了,但是不支持AFAIK。 在我的情况下,我个人的方法是在容器启动时运行一个小脚本,如下所示: 这就是我的的样子: 目前这是工作,但。。。如果我想添加20个(随
我查了错误代码:Err!404许多原因...我在想一个包裹的问题? npm错误!404 npm错误!404'native-react-cli@lates'不在npm注册表中。npm错误!404你应该让作者发布它(或者自己使用这个名字!)npm错误!404 npm错误!404注意,您也可以安装从npm错误!404 tarball、文件夹、http url或git url。 npm错误!此运行的完整日
我有一个函数组件,它返回一个锚点元素,该元素可以具有属性和,这取决于传递给它的: 创建此组件时,我首先尝试通过,而不使用像这样的扩展运算符:
我很困惑,为什么这段代码在我包含一条返回语句的情况下却没有返回语句。任何帮助都将不胜感激! 问题代码: