2017-02-01 2 views
0

Visual Studio 2015 UWP C#에서 게임을합니다. 사용자가 제한 시간 내에 'Ac'로 시작한다고 생각할 수있는 모든 단어를 입력하고 단어마다 점수 변수가 증가합니다. 사전에있는 모든 'Ac'단어를 포함하는 C# UWP 응용 프로그램에 사전 컬렉션이 있습니다. 게임은 잘 작동하지만 사용자가 원하는만큼 여러 번 동일한 단어를 입력 할 수 있으며 점수는 계속 증가합니다. 중복 된 사용자 입력을 처리 할 방법이 있습니까?사용자 입력이 사전 C에서 동일한 지 확인하는 방법

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

이미지가 입력 사용자가 두 번 및 점수 "법"보여줍니다 :이합니다 (DispatcherTimer를 간단히 할 배열의 단어 (400)에 대해 제외하고 제외) 내 코드입니다 계속 증가하면 사용자는 중복 된 단어를 입력 할 수 없습니다.

대신 배열을 사용해 보았지만 사전을 사용하도록 지시했습니다. 이것은 제가 시도한 다른 것들에 대한 링크입니다. https://www.codeproject.com/Questions/1167927/How-to-eliminate-duplicate-user-input-within-the-b

+6

이벤트가 호출 될 때마다 새 사전이 만들어집니다. 이것이 제거가 무효화 된 이유입니다. 이벤트 처리기 외부에서 사전을 초기화하십시오. – Nkosi

+0

Nkosi 감사합니다. 나는 그것을 시도했지만 한 단어 이상을 입력하려고 할 때마다 예외가 발생합니다. 예를 들어 "Act"에 입력 한 다음 "Action"에 입력하려고하면 다음 예외가 발생합니다. "mscorlib.ni.dll에서 'System.ArgumentException'유형의 예외가 발생했지만 사용자 코드에서 처리되지 않았습니다. 추가 정보 : 동일한 키를 가진 항목이 이미 추가되었습니다. 키 : e. " 예외가 발생하면 일반적으로 "e"라는 인스턴스가 하나 이상 있고, ictionary에 선언되어 있지만 하나 이상의 인스턴스가 있으므로 예외가 발생합니다. – wishfulcoder

+0

. 예외는 버튼 밖에서 사전을 선언했을 때만 시작되었습니다. 또한, 버튼 밖에서만 사전을 선언 할 수 있으며 ".adds"는 버튼 내에 있거나 "현재 컨텍스트에 존재하지 않습니다". 같은 :. "사전 WordsWithAc = 새로운 사전 (); 개인 무효 btnEnter_Click (객체 송신자 RoutedEventArgs E) { WordsWithAc.Add ("E "는, 1) 상기 에러가 발생되는 것이다 – wishfulcoder

답변

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

감사합니다. pabsusdev. 그것을 시도했지만 한 단어 이상을 입력하려고 할 때마다 예외가 발생합니다. 예를 들어 "Act"에 입력 할 수 있고 "Action"에 입력하려고하면 다음과 같은 예외가 생깁니다. 동일한 키가 이미 추가되었습니다. Key : e. "일반적으로 예외가 발생하면 사전에 선언 된 'e'인스턴스가 두 개 이상 있지만 인스턴스가 두 개 이상 없으므로 예외가 발생합니다. – wishfulcoder

+0

예외는 버튼 외부에서 사전을 선언했을 때만 발생했습니다. 또한 버튼 밖에서 사전을 선언 할 수 있으며 ".adds"가 버튼 내에 있어야하거나 "현재 컨텍스트에 존재하지 않습니다" 마찬가지로 : "사전 WordsWithAc = 새 사전 (); 개인 무효 btnEnter_Click (개체 보낸 사람, RoutedEventArgs 전자) { WordsWithAc.Add ("e", 1); – wishfulcoder

+0

@wishfulcoder 어디에서 사전을 초기화 했습니까? – AVK

관련 문제