[英]How to check if user input is the same in dictionary c#
我正在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));
}
该图显示用户两次输入了“ Act”,并且乐谱仍然增加,用户不应输入重复的单词。
我曾尝试使用数组来代替,但被指向使用Dictionary。 这是我尝试过的其他事情的链接。 https://www.codeproject.com/Questions/1167927/How-to-eliminate-duplicate-user-input-within-the-b
尝试在事件函数之外声明字典。 不会在每次单击按钮时对其进行初始化。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.