我已经做了这么多,现在有人可以告诉我如何获取记录并将其放入列表中,以便我可以选择访问记录吗?
I've done this much, now can someone tell me how to take the records and put into a list so that i can access the records by choice?
namespace HW
{
class Program
{
struct Employee
{
int join_year;
int age;
string dept;
public void getval(int join_year, int age, string dept)
{
this.join_year = join_year;
this.age = age;
this.dept = dept;
}
public void showval()
{
Console.WriteLine("Joining year of Employee is: {0}", this.join_year);
Console.WriteLine("Age of Employee is: {0}", this.age);
Console.WriteLine("Department of Employee is: {0}", this.dept);
}
}
static void Main(string[] args)
{
Employee emp = new Employee();
Console.Write("Enter Joining Year: ");
int join_year = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Department: ");
string dept = Console.ReadLine();
emp.getval(join_year, age, dept);
emp.showval();
}
}
}
所以是的,基本上我在这里添加了值,它们也被打印出来了。现在我只想把这些记录添加到列表或数组中,以便我可以选择访问记录。
如果我没理解错的话,你想做这个。
List<string>() list = new List<string>();
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: list.Add(join_year.ToString());
break;
case 1: list.Add(age.ToString());
break;
case 2: list.Add(dept);
break;
}
}
它非常简单,但可以完成工作。
namespace HW
{
class Program
{
struct Employee
{
int join_year;
int age;
string dept;
public void getval(int join_year, int age, string dept)
{
this.join_year = join_year;
this.age = age;
this.dept = dept;
}
public void showval()
{
Console.WriteLine("Joining year of Employee is: {0}", this.join_year);
Console.WriteLine("Age of Employee is: {0}", this.age);
Console.WriteLine("Department of Employee is: {0}", this.dept);
}
}
static void Main(string[] args)
{
Employee emp = new Employee();
Console.Write("Enter Joining Year: ");
int join_year = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Department: ");
string dept = Console.ReadLine();
emp.getval(join_year, age, dept);
emp.showval();
}
}
}
所以是的,基本上我在这里添加了值,它们也被打印出来了。现在我只想把这些记录添加到列表或数组中,以便我可以选择访问记录。
如果我没理解错的话,你想做这个。
List<string>() list = new List<string>();
for(int i = 0; i < 3; i++)
{
switch(i)
{
case 0: list.Add(join_year.ToString());
break;
case 1: list.Add(age.ToString());
break;
case 2: list.Add(dept);
break;
}
}
它非常简单,但可以完成工作。