C# 无法隐式转换';System.Collections.Generic.Dictionary<;字符串,System.Collections.Generic.List<;字符串>&燃气轮机';至';系统…<;字符串,字符串>'; 类程序 { 静态void Main(字符串[]参数) { Dictionary questionDict=new Dictionary();//创建动物dict List removeKeys=new List();//因此,如果需要,我可以删除这些键 添加(“它有胡须吗?”,“猫”); 添加(“它发出呼噜声吗?”,“猫”); 添加(“它会叫吗?”,“狗”); while(true) { foreach(questionDict中的KeyValuePair kvp)//检查questionDict中的每个kvp值 { WriteLine(“Computer:{0}”,kvp.Key);//打印kvp,或者在本例中打印问题 字符串userInput=Console.ReadLine(); if(userInput.ToLower()==“yes”)//如果是,则 { WriteLine(“VAL:{0}”,kvp.Value);//写入值 } 其他的 { removeKeys.Add(kvp.Key);//将错误的动物添加到removeKeys列表中 } } foreach(removeKeys中的字符串rKey) { questionDict.Remove(rKey);//从questionDict中删除removeKeys中rKey的所有值 } } } }
C# 无法隐式转换';System.Collections.Generic.Dictionary<;字符串,System.Collections.Generic.List<;字符串>&燃气轮机';至';系统…<;字符串,字符串>'; 类程序 { 静态void Main(字符串[]参数) { Dictionary questionDict=new Dictionary();//创建动物dict List removeKeys=new List();//因此,如果需要,我可以删除这些键 添加(“它有胡须吗?”,“猫”); 添加(“它发出呼噜声吗?”,“猫”); 添加(“它会叫吗?”,“狗”); while(true) { foreach(questionDict中的KeyValuePair kvp)//检查questionDict中的每个kvp值 { WriteLine(“Computer:{0}”,kvp.Key);//打印kvp,或者在本例中打印问题 字符串userInput=Console.ReadLine(); if(userInput.ToLower()==“yes”)//如果是,则 { WriteLine(“VAL:{0}”,kvp.Value);//写入值 } 其他的 { removeKeys.Add(kvp.Key);//将错误的动物添加到removeKeys列表中 } } foreach(removeKeys中的字符串rKey) { questionDict.Remove(rKey);//从questionDict中删除removeKeys中rKey的所有值 } } } },c#,C#,newdictionary()向我提供了错误信息。有什么帮助吗?我试图使我的字典中每个键都有一个以上的值,我被告知这只能通过List实现 将您的声明更改为: class Program { static void Main(string[] args) { Dictionary<string, string> questionDict = new Dictionary<string, List<string>&g
newdictionary()代码>向我提供了错误信息。有什么帮助吗?我试图使我的字典中每个键都有一个以上的值,我被告知这只能通过List
实现 将您的声明更改为:
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
}
}
}
}
这是短得多,更难搞砸 你不明白错误的哪一部分?看看这句话的两面。好吧,你刚才在同一句话中说你想要一本字典。是的,这会使这个错误更难发生(即使我通常不使用var:)。好吧,我非常喜欢它,但即使你不是,你也必须承认这是你可以使用它的最没有争议的方式之一。完全同意!但是,他的代码的其余部分希望字典中的值是单个字符串。@PrestonGuillot是的,此更改需要传播到他的代码的其余部分。
Dictionary<string, List<string>> questionDict = new Dictionary<string, List<string>>();
var questionDict = new Dictionary<string, List<string>>();