2013-05-25 47 views
0

我正在製作一個控制檯程序,其中有多個值映射到dictionarykeyLookup。我使用的是使用該鍵的命令來輸出console.writeline = ("stuff");,但它只有在我的鍵值和鍵值相同時(字典中才有效)。我不知道這是爲什麼。我一直在使用listforeach以及一些變量試圖弄清楚我做錯了什麼,但即使它繼續工作,它現在的工作方式仍然無法工作。具有多個鍵的字典無法正常工作

另外,如果我的console.readline();中有一個詞不在我的詞典中,那麼整個事情就會崩潰。我不想要的,我不確定它爲什麼要這樣做,或者在某些時候它不這樣做。此外,我的mathFunction字典的工作原理是如何讓我的keyLookup字典正常工作。雖然我認爲區別在於我如何使用列表通過keyLookup來交叉引用。

class MainClass 
    { 
     public static string Line; 
     static string foundKey; 
     public static void Main (string[] args) 
     { 
      while (true) 
      { 
       if (Line == null) 
       {Console.WriteLine ("Enter Input"); } 
       WordChecker(); 
      } 
    } 
    public static void WordChecker() 
    { 
     string inputString = Console.ReadLine(); 
     inputString = inputString.ToLower(); 

     string[] stripChars = { ";", ",", ".", "-", "_", "^", "(", ")", "[", "]", 
      "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\n", "\t", "\r" }; 

     foreach (string character in stripChars) 
     { 
      inputString = inputString.Replace(character, ""); 
     } 
     // Split on spaces into a List of strings 
     List<string> wordList = inputString.Split(' ').ToList(); 
     // Define and remove stopwords 
     string[] stopwords = new string[] { "and", "the", "she", "for", "this", "you", "but" }; 

     foreach (string word in stopwords) 
     { 
      // While there's still an instance of a stopword in the wordList, remove it. 
      // If we don't use a while loop on this each call to Remove simply removes a single 
      // instance of the stopword from our wordList, and we can't call Replace on the 
      // entire string (as opposed to the individual words in the string) as it's 
      // too indiscriminate (i.e. removing 'and' will turn words like 'bandage' into 'bdage'!) 
      while (wordList.Contains(word)) 
      { 
       wordList.Remove(word); 
      } 
     } 
     // Create a new Dictionary object 
     Dictionary<string, int> dictionary = new Dictionary<string, int>(); 

     // Loop over all over the words in our wordList... 

     foreach (string word in wordList) 
     { 
      // If the length of the word is at least three letters... 

      if (word.Length >= 3) 
      { 

       // ...check if the dictionary already has the word. 

       if (dictionary.ContainsKey(word)) 
       { 

        // If we already have the word in the dictionary, increment the count of how many times it appears 

        dictionary[word]++; 
       } 
       else 
       { 
        // Otherwise, if it's a new word then add it to the dictionary with an initial count of 1 

        dictionary[word] = 1; 
       } 
      } 

      List<string> dicList = new List<string>(); 
      dicList = dictionary.Keys.ToList(); 

      Dictionary<string, string> keyLookup = new Dictionary<string, string>(); 

      keyLookup["hey"] = "greeting"; 
      keyLookup["hi"] = "greeting"; 
      keyLookup["greeting"] = "greeting"; 
      keyLookup["math"] = "math"; 
      keyLookup["calculate"] = "math"; 
      keyLookup["equation"] = "math"; 

      foundKey = keyLookup[word]; 

      List<string> keyList = new List<string>(); 

      foreach (string keyWord in dicList) 
      { 
       if(keyWord == foundKey) 
       {keyList.Add (keyWord); } 
      } 

      foreach (string mKey in keyList) 
      { 
      if(mKey == "greeting") 
      {Greetings();} 

      if (mKey == "math") 
      {Math();} 
      } 
     } 
    } 

    public static void Math() 
    { 
     Console.WriteLine ("What do you want me to math?"); 
     Console.WriteLine ("input a number"); 
     string input = Console.ReadLine(); 

     decimal a = Convert.ToDecimal (input); 
     Console.WriteLine("Tell me math function"); 
     string mFunction = Console.ReadLine(); 

     Console.WriteLine ("tell me another number"); 
     string inputB = Console.ReadLine(); 
     decimal b = Convert.ToDecimal (inputB); 

     Dictionary<string, string> mathFunction = new Dictionary<string, string>(); 

     mathFunction["multiply"] = "multiply"; 
     mathFunction["times"] = "multiply"; 
     mathFunction["x"] = "multiply"; 
     mathFunction["*"] = "multiply"; 
     mathFunction["divide"] = "divide"; 
     mathFunction["/"] = "divide"; 
     mathFunction["subtract"] = "subtract"; 
     mathFunction["minus"] = "subtract"; 
     mathFunction["-"] = "subtract"; 
     mathFunction["add"] = "add"; 
     mathFunction["+"] = "add"; 
     mathFunction["plus"] = "add"; 

     string foundKey = mathFunction[mFunction]; 

     if (foundKey == "add") 
     { 
      Console.WriteLine (a + b); 
     } 
     else if (foundKey == "subtract") 
     { 
      Console.WriteLine (a - b); 
     } 
     else if (foundKey == "multiply") 
     { 
      Console.WriteLine (a * b); 
     } 
     else if (foundKey == "divide") 
     { 
      Console.WriteLine (a/b); 
     } 
     else 
     { 
      Console.WriteLine ("not a math"); 
     } 
    } 

    public static void Greetings() 
    { 
     Console.WriteLine("You said hello"); 
    } 
}' 
+0

這是一個非常複雜的問題。你想要達到什麼目的?如果你可以更清楚地解釋你要找的結果,那麼肯定有一種方法可以使用LINQ來完成所有這些工作。 –

+0

我正在嘗試製作一個程序,讓我可以在「對話式時尚」中輸入行,並讓它抓住我所說的和執行功能的關鍵字。比如我現在有數學計算。後來我想嘗試讓它做更復雜的事情,但首先我需要正確地讀取我的輸入。 是的我知道它可以用較少的代碼完成。我是一個總noob雖然:) – AmazingMrBrock

+0

我打算進一步看看,但這個代碼需要重新寫入。我發現兩個錯誤和幾個地方你可以使用謂詞函數。 –

回答

1

你應該以不同的方式遍歷字典(不要使用ToList-Function)。

試試這個:

foreach (KeyValuePair kvp (Of String, String) In testDictionary) 
{ 
    Debug.WriteLine("Key:" + kvp.Key + " Value:" + kvp.Value); 
} 

以及應用程序崩潰,因爲這個代碼,如果字不匹配,(你不是創建一個新的條目,方式):

// Otherwise, if it's a new word then add it to the dictionary with an initial count of 1 
dictionary[word] = 1; 

編輯:我錯了,dictionary[word] = 1不會創建一個新的元素。這是非常好的。

+0

在我嘗試之前,我必須做一些研究。我不熟悉'KeyValuePair''dem'或'Next'。謝謝你的提示。我會確定閱讀並嘗試弄清楚如何實現它。 – AmazingMrBrock

+0

您也可以使用GetEnumerator並通過它循環,如果您更熟悉這一點。 –

+0

'字典[單詞] = 1;'是正確的。 http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx。 '如果沒有找到指定的鍵,get操作拋出一個KeyNotFoundException異常,** set操作用指定的鍵**創建一個新元素。' – I4V

0

foundKey = keyLookup[word];

如果word不存在keyLookup那麼它將會崩潰。

string foundKey = mathFunction[mFunction];

如果mFunction不存在mathFunction那麼它將會崩潰。


如果你想使這個「對話」節目,那麼這個詞查找最重要的部分。你不使用謂詞或LINQ,都可以使字符串函數極其容易。目前你使用一個字典。爲什麼不爲每個關鍵字使用列表?

List<string> GreetingKeywords; 
GreetingKeywords.Add("hello"); // ... 

List<string> MathKeywords; 
MathKeywords.Add("math"); // ... 

foreach (var word in dicList) 
{ 
    if (GreetingKeywords.Contains(word)) 
    { Greetings(); } 
    if (MathKeywords.Contains(word)) 
    { Maths(); } 
} 

我建議你謂詞念起來列表/詞典功能,如查找,的IndexOf,等等,等等這些知識是非常寶貴的C#。