【发布时间】:2020-04-01 23:32:15
【问题描述】:
int userInput = Convert.ToInt32(Console.ReadLine());
foreach (KeyValuePair<int,Library> lib in dictionary)
{
if (userInput == lib.Key)
{
Console.WriteLine("You chose book = {0}",lib.Value.bookName);
}
else
{
Console.WriteLine("Wrong Input");
}
}
您好,我一直在弄清楚如何在通过字典循环时检查用户输入(ReadLine)与字典中的键的相等性,而不会发送垃圾邮件“错误输入”的其他命令。
【问题讨论】:
-
只使用 public bool ContainsKey (TKey key); docs.microsoft.com/en-us/dotnet/api/… 不需要 foreach(或其他循环)
标签: c# loops dictionary