c# - 检查单词是否正确后出现字典错误
问题描述
我用一些单词创建字典,从英语翻译成波兰语,从波兰语翻译成英语。从英语到波兰语的作品,它看起来像这样:
Dictionary<string, string> myDict =
new Dictionary<string, string>();
myDict.Add("Pies", "Dog");
myDict.Add("Kot", "Cat");
myDict.Add("Swinia", "Pig");
myDict.Add("Chomik", "Hamster");
myDict.Add("Ptak", "Bird");
myDict.Add("Mysz", "Mouse");
myDict.Add("Pszczola", "Bee");
myDict.Add("Niedzwiedz", "Bear");
myDict.Add("Krowa", "Cow");
myDict.Add("Kurczak", "Chicken");
myDict.Add("Kaczka", "Duck");
myDict.Add("Slon", "Elephant");
myDict.Add("Ryba", "Fish");
myDict.Add("Zaba", "Frog");
myDict.Add("Lew", "Lion");
if (textBox2.Text == myDict[textBox1.Text])
{
MessageBox.Show("Dobrze");
var rnd = new Random();
int randomValue = rnd.Next(1, 15);
string a = myDict.Values.ElementAt(randomValue);
textBox2.Text = a;
}
但是从波兰语到英语不起作用,看起来像这样。
键入一个单词并检查后,弹出错误:
System.Collections.Generic.KeyNotFoundException:“给定的密钥不在字典中。”
Dictionary<string, string> myDict =
new Dictionary<string, string>();
myDict.Add("Pies", "Dog");
myDict.Add("Kot", "Cat");
myDict.Add("Swinia", "Pig");
myDict.Add("Chomik", "Hamster");
myDict.Add("Ptak", "Bird");
myDict.Add("Mysz", "Mouse");
myDict.Add("Pszczola", "Bee");
myDict.Add("Niedzwiedz", "Bear");
myDict.Add("Krowa", "Cow");
myDict.Add("Kurczak", "Chicken");
myDict.Add("Kaczka", "Duck");
myDict.Add("Slon", "Elephant");
myDict.Add("Ryba", "Fish");
myDict.Add("Zaba", "Frog");
myDict.Add("Lew", "Lion");
if (textBox2.Text == myDict[textBox1.Text])
{
MessageBox.Show("Dobrze");
var rnd = new Random();
int randomValue = rnd.Next(1, 15);
string b = myDict.Keys.ElementAt(randomValue);
textBox2.Text = b;
}
我只改变
string a = myDict.Values.ElementAt(randomValue);
至
string b = myDict.Keys.ElementAt(randomValue);
解决方案
推荐阅读
- python - Pandas 使用列表中的值替换字符串
- r - 如何使用滑块为 R 扩展的 r:get 调用设置参数?
- python - 用邻居的平均值替换我的数据集的异常值
- django - Django 是否缓存数据库的 DNS 信息?
- java - 如何知道新文件是否添加到谷歌驱动器中的特定文件夹
- javascript - 如何仅突出显示 HTML 元素的某些单词?
- python - 将具有多个时区的 pandas 列转换为单个时区
- javascript - Firefox 不能一次下载多个文件?
- python-3.x - 从列表中获取子列表的组合
- python - 如何在fabric 2.4中指定远程shell