[英]Cannot break out the foreach loop
我正在使用集合添加員工列表,添加並顯示 function,如下所示:
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;
}
}
還有一個包含菜單的 class:
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 打到 go 回到主菜單並選擇顯示員工,它一直在打印我的員工列表並且無法中斷循環。 我該如何解決這個問題,我試圖在 importemployee() 菜單中顯示,它沒有問題。
Display() 中 foreach 循環的中斷是可以的,那里沒有錯。 “ break; ” 跳出最近的循環或 switch 語句。
您的問題是您無法在 MainMenu() 中跳出 do-while 並且您的選擇將保持為 2,因為您沒有在 do-while 中讀取輸入。
換句話說,您將在 Display() 中跳出 foreach,最終進入 MainMenu() 中的 switch,您將跳出 switch,然后重新開始 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()”中還有一個相當大的問題,當您修復主菜單循環時會很明顯。 如果您稍后需要提示,我會在評論中發布,但請嘗試自己找到。
總帳
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.