首页 > 解决方案 > 如何使用 forloop 实例化/添加一些员工(全名为 Joe)到列表中?

问题描述

注意:此问题已修改,因此如果您正在阅读回复,请记住该问题以前要求为每个员工提供不同的名字数据。现在他们都叫乔。

我创建了一个 foreachloop,它应该遍历多个列表(每个属性一个列表),并用 Joe 的名字、姓氏和 ID 号实例化一个员工对象。所有员工的名字都必须是“Joe”,但姓氏和身份证号码不同。然后它将每个新员工添加到名为“员工”的列表中。我已经设置了名字、姓氏和 ID 列表,但是当我尝试将员工列表打印到控制台时,它只打印了 5 次员工的信息。该循环似乎只执行每个列表中的第一项。这对于名字是预期的(Joe 是列表中的唯一项目),但它应该每次都移至其他姓氏和 ID 号。

另外:许多人说 foreach 不是解决此问题的好方法;但我的任务特别要求我使用 foreach 循环来填充这个列表。我不确定将他们都命名为 Joe 是否会使这或多或少变得复杂,但这就是我需要做的。

Employee 是它自己的类,列表在程序文件中。我在下面都粘贴了。答案可能很明显,但如果您能看看可能出了什么问题,我将不胜感激。我需要 foreach 循环为属性列表中的每个项目创建一个员工对象,然后将其信息打印到控制台。非常感谢!(感谢您在编辑前的回复。我现在正在测试您的解决方案,并将在我这样做时更新!)

员工档案:

namespace CSDrill_Loop
{
    public class Employee
    {
        public string firstname { get; set; }
        public string lastname { get; set; }
        public int ID { get; set; }

    }
}

程序文件:

namespace CSDrill_Loop
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> Employees = new List<Employee>();
            List<string> firstnames = new List<string>()
            {
                "Joe"
            };

            List<string> lastnames = new List<string>()
            {
                "Jackson", "Smith", "Miller", "Turner", "Johnson"
            };

            List<int> IDs = new List<int>()
            {
                34332, 54754, 43523, 87012, 43158
            };

            foreach (string firstname in firstnames)
            {
                foreach (string lastname in lastnames)
                {
                    foreach (int ID in IDs)
                    {

                        Employee Employeeobject = new Employee();
                        Employeeobject.firstname = firstname;
                        Employeeobject.lastname = lastname;
                        Employeeobject.ID = ID;
                        Employees.Add(Employeeobject);


                        foreach (Employee Employee in Employees)
                        {
                            Console.WriteLine(Employeeobject.firstname + " "+Employeeobject.lastname+" "+Employeeobject.ID);
                            
                        }
                       Console.ReadLine();
                    }
                }
            }
        }
    }
}

标签: c#loopsclassforeach

解决方案


它应该更像

**Employee file:**

namespace CSDrill_Loop
{
    public class Employee
    {
        public string firstname { get; set; }
        public string lastname { get; set; }
        public int ID { get; set; }
        public Employee(string _firstName, string _lastName, int _ID)
        {
              firstname = _firstName;
              lastName = _lastName;
              ID = _ID;
        }
    }
}


**Program file:**

namespace CSDrill_Loop
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> Employees = new List<Employee>();
            List<string> firstnames = new List<string>()
            {
                "Bob", "Jeff", "Dale", "Kate", "Ann"
            };

            List<string> lastnames = new List<string>()
            {
                "Jackson", "Smith", "Miller", "Turner", "Johnson"
            };

            List<int> IDs = new List<int>()
            {
                34332, 54754, 43523, 87012, 43158
            };
            for(int i = 0; i < firstNames.length; i++)
            {
                Employees.Add(new Employee(firstNames[i], lastNames[i], IDs[i]));
            }
        }
    }
}

如果你只能使用 foreach 那么

            int i = 0;
            foreach(var firstName in firstNames)
            {
                Employees.Add(new Employee(firstName, lastNames[i], IDs[i]));
                i++;
            }

推荐阅读