C# 如何在字典c中检查用户输入是否相同#

C# 如何在字典c中检查用户输入是否相同#,c#,duplicates,uwp,C#,Duplicates,Uwp,我正在Visual Studio 2015 UWP C#中制作一个游戏,用户在时间限制内输入他们能想到的以“Ac”开头的所有单词,每个单词的分数都会增加。我的C#UWP应用程序中有一个字典集合,其中包含字典中的所有“Ac”单词。游戏运行良好,唯一的问题是用户可以输入同一个单词任意多次,分数仍然会增加。有没有办法处理重复的用户输入?这是我的代码(不包括数组中大约400个简短的单词,也不包括Dispatcher): private void b输入\u单击(对象发送者,路由目标) { 字典单词swi

我正在Visual Studio 2015 UWP C#中制作一个游戏,用户在时间限制内输入他们能想到的以“Ac”开头的所有单词,每个单词的分数都会增加。我的C#UWP应用程序中有一个字典集合,其中包含字典中的所有“Ac”单词。游戏运行良好,唯一的问题是用户可以输入同一个单词任意多次,分数仍然会增加。有没有办法处理重复的用户输入?这是我的代码(不包括数组中大约400个简短的单词,也不包括Dispatcher):

private void b输入\u单击(对象发送者,路由目标)
{
字典单词swithac=新字典();
添加(“e”,1);
加上(“t”,2);
添加(“ed”,3);
添加(“es”,4);
加上(“他”,5);
加上(“hy”,6);
添加(“id”,7);
添加(“我”,8);
添加(“ne”,第9页);
添加(“re”,10);
如果(GlobalClassAttention.totalRecallScore>=150)
{
btnLevel2.Visibility=可见性.Visibility;
}
if(WordsWithAc.ContainsKey(txtexrinput.Text))
{
GlobalClassAttention.totalRecallScore+=10;
txtScore.Text=GlobalClassAttention.totalRecallScore.ToString();
imgCorrectSign.Visibility=可见性.Visibility;
imgX.Visibility=Visibility.Collapsed;
WordsWithAc.Remove(txtUserInput.Text);//不会将其从字典中删除,以便用户可以多次输入同一个单词
}
其他的
{
imgX.Visibility=可见性.Visibility;
imgCorrectSign.Visibility=可见性。已折叠;
}
}
私有void btnLevel2_单击(对象发送者,路由目标e)
{
帧导航(typeof(TotalRecallLevel2));
}

图中显示,用户在“Act”中输入了两次,并且分数仍在增加,用户不应输入重复的单词

我尝试过使用数组,但被指向使用字典。这是我尝试过的其他东西的链接。

尝试在事件函数之外声明字典。不会在每次单击按钮时对其进行初始化

Dictionary<string, int> WordsWithAc = new Dictionary<string, int>();
    WordsWithAc.Add("e", 1);
    WordsWithAc.Add("t", 2);
    WordsWithAc.Add("ed", 3);
    WordsWithAc.Add("es", 4);
    WordsWithAc.Add("he", 5);
    WordsWithAc.Add("hy", 6);
    WordsWithAc.Add("id", 7);
    WordsWithAc.Add("me", 8);
    WordsWithAc.Add("ne",9);
    WordsWithAc.Add("re",10);
private void btnEnter_Click(object sender, RoutedEventArgs e)
{


    if (GlobalClassAttention.totalRecallScore >= 150)
    {
        btnLevel2.Visibility = Visibility.Visible;
    }

    if (WordsWithAc.ContainsKey(txtUserInput.Text))
    {
        GlobalClassAttention.totalRecallScore += 10;
        txtScore.Text=GlobalClassAttention.totalRecallScore.ToString();
        imgCorrectSign.Visibility = Visibility.Visible;
        imgX.Visibility = Visibility.Collapsed;
        WordsWithAc.Remove(txtUserInput.Text); //Doesn't remove it from the dictionary so the user can enter in the same word more than once
    }
    else
    {
        imgX.Visibility = Visibility.Visible;
        imgCorrectSign.Visibility = Visibility.Collapsed;
    }
}
Dictionary WordsWithAc=newdictionary();
添加(“e”,1);
加上(“t”,2);
添加(“ed”,3);
添加(“es”,4);
加上(“他”,5);
加上(“hy”,6);
添加(“id”,7);
添加(“我”,8);
添加(“ne”,第9页);
添加(“re”,10);
私有void b输入\单击(对象发送方,路由目标)
{
如果(GlobalClassAttention.totalRecallScore>=150)
{
btnLevel2.Visibility=可见性.Visibility;
}
if(WordsWithAc.ContainsKey(txtexrinput.Text))
{
GlobalClassAttention.totalRecallScore+=10;
txtScore.Text=GlobalClassAttention.totalRecallScore.ToString();
imgCorrectSign.Visibility=可见性.Visibility;
imgX.Visibility=Visibility.Collapsed;
WordsWithAc.Remove(txtUserInput.Text);//不会将其从字典中删除,以便用户可以多次输入同一个单词
}
其他的
{
imgX.Visibility=可见性.Visibility;
imgCorrectSign.Visibility=可见性。已折叠;
}
}

每次调用事件时,都会创建一个新字典。这就是移除被否定的原因。在事件处理程序外部初始化字典。谢谢。我试过了,但每次我试着输入一个以上的单词时都会出现异常。例如,我可以在“Act”中输入,然后如果我尝试在“Action”中输入,我会得到以下异常:“mscorlib.ni.dll中发生了类型为“System.ArgumentException”的异常,但未在用户代码中处理。其他信息:已添加具有相同密钥的项。密钥:e。”通常情况下,当异常发生时,这意味着我有一个以上的“e”实例,在ictionary中声明,但我没有一个以上的“e”实例,因此其他原因导致了该异常。。当我在按钮外声明字典时,异常才开始发生。此外,我只能在按钮外声明字典,“.adds”必须在按钮内,否则它们“在当前上下文中不存在”。类似这样:“Dictionary WordsWithAc=new Dictionary();private void bEnter_Click(object sender,RoutedEventArgs e){WordsWithAc.Add(“e”,1);这就是导致错误的原因。它正在尝试向字典添加现有的键。在外部创建字典。请在构造中填充字典,或在将只调用一次的其他方法中填充字典,不要将其添加回事件处理程序中。谢谢pabsusdev。我尝试过,但每次尝试时都会出现异常输入多个单词。例如,我可以输入“Act”,如果我尝试输入“Action”,则会出现以下异常:“已添加具有相同键的项。键:e。"通常,当异常发生时,这意味着我在字典中声明了多个“e”实例,但我没有多个“e”实例,因此其他原因导致了该异常。只有在我在按钮外声明字典时,异常才开始发生。此外,我只能在按钮外声明字典on和“.adds”必须在按钮内,否则它们“在当前上下文中不存在”。例如:“Dictionary WordsWithAc=new Dictionary();private void bEnter_Click(object sender,RoutedEventTargets e){WordsWithAc.Add(“e”,1);@wishfulcoder您在哪里初始化了词典?您还应该将“adds”放在按钮事件之外。这就是为什么您会得到
Dictionary<string, int> WordsWithAc = new Dictionary<string, int>();
    WordsWithAc.Add("e", 1);
    WordsWithAc.Add("t", 2);
    WordsWithAc.Add("ed", 3);
    WordsWithAc.Add("es", 4);
    WordsWithAc.Add("he", 5);
    WordsWithAc.Add("hy", 6);
    WordsWithAc.Add("id", 7);
    WordsWithAc.Add("me", 8);
    WordsWithAc.Add("ne",9);
    WordsWithAc.Add("re",10);
private void btnEnter_Click(object sender, RoutedEventArgs e)
{


    if (GlobalClassAttention.totalRecallScore >= 150)
    {
        btnLevel2.Visibility = Visibility.Visible;
    }

    if (WordsWithAc.ContainsKey(txtUserInput.Text))
    {
        GlobalClassAttention.totalRecallScore += 10;
        txtScore.Text=GlobalClassAttention.totalRecallScore.ToString();
        imgCorrectSign.Visibility = Visibility.Visible;
        imgX.Visibility = Visibility.Collapsed;
        WordsWithAc.Remove(txtUserInput.Text); //Doesn't remove it from the dictionary so the user can enter in the same word more than once
    }
    else
    {
        imgX.Visibility = Visibility.Visible;
        imgCorrectSign.Visibility = Visibility.Collapsed;
    }
}