class Program
{
static void Main(string[] args)
{
Dictionary<string, string> questionDict = new Dictionary<string, List<string>>(); //creating animal dict
List<string> removeKeys = new List<string>(); //so I can remove the keys if need be
questionDict.Add("Does it have whiskers?", "cat");
questionDict.Add("Does it purr?", "cat");
questionDict.Add("Does it bark?", "dog");
while (true)
{
foreach (KeyValuePair<string, string> kvp in questionDict)//checks for each value of kvp in questionDict
{
Console.WriteLine("Computer: {0}", kvp.Key); //prints kvp, or in this instance, the question
string userInput = Console.ReadLine();
if (userInput.ToLower() == "yes") //if yes THEN
{
Console.WriteLine("VAL: {0}", kvp.Value); //writes the value
}
else
{
removeKeys.Add(kvp.Key); //adds the wrong animals to the removeKeys list
}
}
foreach(string rKey in removeKeys)
{
questionDict.Remove(rKey); //removes all the values of rKey in removeKeys from questionDict
}
}
}
}
new Dictionary<string, List<string>>();
是給我的錯誤。任何幫助?我試圖讓我的字典每個鍵有多個值,我被告知只能通過List<string>
來實現。無法隱式轉換 'System.Collections.Generic.Dictionary <字符串,System.Collections.Generic.List <string>>' 到 '系統... <字符串,字符串>'
錯誤的哪部分你不明白?看看那條線的兩邊。 – SLaks 2015-02-05 21:17:29
那麼,你剛剛在同一行上說過你想要一個字典。 –
2015-02-05 21:17:40