无法打破foreach循环

时间:2019-09-28 13:24:28

标签: .net console-application

我正在使用集合来添加具有如下所示的添加和显示功能的员工列表:

class Collection<T>
{
    static List<T> list = new List<T>();
    public Collection()
    {

    }

    public bool Add(T obj, ref T u)
    {
        list.Add(obj);  
        return true;
    }

    public void Display()
    {
        bool s = false;
        foreach (T u in list)
        {
            Console.WriteLine(u.ToString());
            s = true;
            break;
        }         
    }

还有一个包含菜单的类:

public class EmployeeManagement : Employee
{
    Collection<Employee> emp = new Collection<Employee>();
    public void MainMenu()

    {
        Console.WriteLine("1. Import Employee");
        Console.WriteLine("2. Display Employee Information");
        Console.WriteLine("3. Search Employee");
        Console.WriteLine("4. Exit");
        Console.WriteLine("Enter Menu Option Number: ");
        int choice = Convert.ToInt32(Console.ReadLine());
        do
        {
            switch(choice)
            {
            case 1:
                ImportEmployee();
                break;
            case 2:
                emp.Display();
                break;
            }
        } while (true);


    }

    public void AddEmployee(Employee u)
    {
        u.InputEmployee();           
        if (!emp.Add(u))
        {
            Console.WriteLine("88");
        }
    }


    public void ImportEmployee()
    {

        while (true)
        {

            Console.WriteLine("================== Import Employee ============");
            Console.WriteLine("1. Salaried Employee");
            Console.WriteLine("2. Hourly Employee");
            Console.WriteLine("3. Main Menu");
            int choice = Convert.ToInt32(Console.ReadLine());
            if (choice == 1)
            {
                SalariedEmployee salEmployee = new SalariedEmployee();
                AddEmployee(salEmployee);
            }
            else if (choice == 2)
            {
                HourlyEmployee hourlyEmployee = new HourlyEmployee();
                AddEmployee(hourlyEmployee);
            }
            else
            {
                MainMenu();
                break;
            }
            Console.ReadKey();
        }

    }
}

我已经添加了一个新员工,并且已经添加到我的列表中。之后,我点击选项3返回主菜单并选择显示员工,它将继续打印我的列表员工,并且无法中断循环。我该如何解决此问题,我尝试在importemployee()菜单中显示它,但没有任何问题。

1 个答案:

答案 0 :(得分:0)

在Display()中foreach循环中的中断是可以的,那里没有错。 “ break;”脱离最近的循环或switch语句。

您的问题是,您无法在MainMenu()中退出do-while,并且您的选择将保持2,因为您没有在do-while中读取输入。

换句话说,您将脱离Display()中的foreach,最终将进入MainMenu()中的开关,您将脱离该开关,开始执行do-while再次,然后您将进入Display(),因为您的选择仍然是2。这将不断重复。

我为您的代码写了一些注释,以便您可以更轻松地遵循流程并添加了一些建议。

    public void Display()
    {
        bool s = false;
        foreach (T u in list)
        {
            Console.WriteLine(u.ToString());
            s = true;

            //Breaks out of this loop, nothing wrong here. I would use return though, if I don't have to perform any code afterwards.
            break;
        }         
    }
   public void MainMenu()
   {
        .
        .
        .

        //Only reading choice once. Would need to handle exception
        int choice = Convert.ToInt32(Console.ReadLine());
        do
        {
            //Choice will always be what you initially selected since you are not reading it inside the do-while. If you selected two it will always be two.
            switch(choice)
            {
            case 1:
                ImportEmployee();

                //Breaks out of switch statement
                break;
            case 2:
                emp.Display();

                //Breaks out of switch statement
                break;
            }

        //Infinity loop since you have no breaks for the do-while or a condition to exit it
        } while (true);
   }

如果您想使用do-while,我对您的do-while的建议是像这样。

        public void MainMenu()
        {
            Console.WriteLine("1. Import Employee");
            Console.WriteLine("2. Display Employee Information");
            Console.WriteLine("3. Search Employee");
            Console.WriteLine("4. Exit");
            Console.WriteLine("Enter Menu Option Number: ");

            //Default value 0
            int choice;
            do
            {
                //Use TryParse, it safely tries to interpret a string as a number. For string "s" it would return false, for string -1 it would return -1.
                //If an invalid string is entered, it will display a message, set choice to 0, go to the end of the loop then start over reading a key.
                //The entered string needs to be between the value of 1 and 4
                if (!int.TryParse(Console.ReadLine(), out choice) || choice > 4 || choice < 1)
                {
                    //We do not want to automatically trigger our switch with our previous value
                    choice = 0;

                    Console.WriteLine("Invalid menu selection, select 1-4");
                }

                switch (choice)
                {
                    case 1:
                        ImportEmployee();
                        break;
                    case 2:
                        emp.Display();
                        break;
                    case 3:
                        Console.WriteLine("Not implemented");
                        break;
                }
            } while (choice != 4); //Only stop the loop if we selected 4
        }

执行此操作的方法有很多,但这感觉就像是在做作业,因此我只是在回答您的问题,而不建议其他改进。

您的“ ImportEmployee()”中也有一个很大的问题,当您修复主菜单循环时,这一问题将很明显。如果以后需要提示,我会在评论中发布,但请尝试自己找到它。

GL