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

时间:2017-02-01 17:00:51

标签: c# duplicates uwp

我正在使用Visual Studio 2015 UWP C#制作游戏,用户在时间限制内输入他们可以想到的所有单词“Ac”,并且每个单词的分数变量都会递增。我的C#UWP应用程序中有一个Dictionary Collection,包含字典中的所有'Ac'字样。游戏运行良好,唯一的事情是用户可以根据需要多次输入相同的单词,分数仍然会增加。有没有办法处理重复的用户输入?这是我的代码(不包括数组中大约400个单词的简短且不包括DispatcherTimer):

    private void btnEnter_Click(object sender, RoutedEventArgs e)
    {
        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); 

        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;
        }
    }

    private void btnLevel2_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(TotalRecallLevel2));
    }

wordserror.png

图像显示用户两次输入“Act”并且分数仍然增加,用户不能输入重复的单词。

我尝试使用数组,但指向使用Dictionary。这是我尝试过的其他事情的链接。 https://www.codeproject.com/Questions/1167927/How-to-eliminate-duplicate-user-input-within-the-b

1 个答案:

答案 0 :(得分:1)

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

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;
    }
}