[英]Do not check in the generic list of a item there is
我想申請一個小保險。 應用程序會詢問您的姓名,然后檢查姓名是否已存在; 如果它們存在,那么您會收到一條消息,此名稱已被使用。
如果沒有,那么您將被問到您想要什么保險,然后在屏幕上您會收到一條消息,該名稱已正確注冊。
我有一個函數可以列出我所有客戶的姓名和保險類型作為通用列表
我制作了一個通用列表,並在我的函數中使用 foreach 以便我可以打印通用列表中的每個項目,一切正常,但是如果我將一個人添加到我的列表中,然后我嘗試回寫相同的名稱,我不會出錯。
問題:為什么我沒有收到錯誤消息? 實際上有什么問題?
先感謝您
我的代碼在這里:
public static List<string> klantgegevens = new List<string>();
public static void Main(string[] args)
{
retry:
string naam;
int verzekeringkeuze;
string alleklantgegevens;
Console.WriteLine("wat is u naam");
naam = Console.ReadLine();
if (klantgegevens.Contains(naam))
{
Console.WriteLine("deze naam bestaat al");
}
else
{
Console.WriteLine("kies u soort verzekering: 1-auto,2-leven,3-woning");
verzekeringkeuze = int.Parse(Console.ReadLine());
switch (verzekeringkeuze)
{
case 1:
Console.WriteLine(alleklantgegevens = $"{naam}:auto");
break;
case 2:
Console.WriteLine(alleklantgegevens = $"{naam}:leven");
break;
case 3:
Console.WriteLine(alleklantgegevens = $"{naam}:woning");
break;
default:
break;
}
Console.WriteLine($"{naam} werd correct ingeschreven");
Console.ReadLine();
alleklantgegevens = $"{naam}:{verzekeringkeuze}";
klantgegevens.Add(alleklantgegevens);
}
Printall();
goto retry;
}
public static void Printall()
{
foreach (string gegevens in klantgegevens)
{
Console.WriteLine($"alle klantgegevens:{gegevens}");
}
Console.ReadLine();
}
沒有錯誤,因為您檢查 Contains(naam) 而不是 $"{naam}:{verzekeringkeuze}" 這是您放入列表中的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.