如何从字典中检查特定字符串的键

时间:2018-10-21 01:33:19

标签: c# dictionary unity3d

目前,我正在做一个小型问答游戏,其中找出我想使用词典的正确答案,并用键和'if'语句检查正确答案。代码如下,但是基本上我是从SQLite数据库中获取答案,然后将它们放在字典中,然后将其转换为列表,将其改组,然后将列表中的每个组件放在文本中以不同方式显示它们UI按钮。在这里:

string A_p = reader.GetString(3);
string B_p = reader.GetString(4);
string C_p = reader.GetString(5);
string D_p = reader.GetString(6);





Dictionary<string, string> dictionaryAnswers = new Dictionary<string, string>();
dictionaryAnswers[A_p] = "C";
dictionaryAnswers[B_p] = "W";
dictionaryAnswers[C_p] = "W";
dictionaryAnswers[D_p] = "W";                       
listAnswers = dictionaryAnswers.Keys.ToList();

var randomizer = new System.Random();
for (int i = 0; i < dictionaryAnswers.Count / 2; i++)
{
    var randNum = randomizer.Next(i, listAnswers.Count);
    var temp = listAnswers[randNum];
    listAnswers[randNum] = listAnswers[i];
    listAnswers[i] = temp;
}

string A = listAnswers.First();
listAnswers.Remove(A);
string B = listAnswers.First();
listAnswers.Remove(B);
string C = listAnswers.First();
listAnswers.Remove(C);
string D = listAnswers.First();
listAnswers.Remove(D);





QuestionText.text = question;
AnswerAText.text = A;
AnswerBText.text = B;
AnswerCText.text = C;
AnswerDText.text = D;

如果代码不可理解,请告诉我。问题是,我不知道如何检查此字符串(A,B,C,D)中包含我正在寻找的密钥。有人可以请我指导我如何做我想做的事。谢谢。

1 个答案:

答案 0 :(得分:0)

foreach (KeyValuePair<string, string> item in dictionaryAnswers)
{
    Debug.Log(item.Key);
    Debug.Log(item.Value);
}