[英]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.