Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有多个键的字典是';他没有按预期工作_C#_List_Dictionary_Foreach - Fatal编程技术网

C# 具有多个键的字典是';他没有按预期工作

C# 具有多个键的字典是';他没有按预期工作,c#,list,dictionary,foreach,C#,List,Dictionary,Foreach,我正在制作一个控制台程序,其中有多个值映射到dictionarykeyLookup。我正在使用if命令,这些命令使用键输出一些console.writeline=(“stuff”)但它只有在值和键(在字典中)相同的情况下才起作用。我不知道这是为什么。我一直在玩弄list和foreach以及一些变量,试图找出我做错了什么,但即使它现在继续工作,它仍然不能按我想要的方式工作 另外,如果我的console.readline()中有一个单词=3) { //…检查字典中是否已有该词。 if(字典.Cont

我正在制作一个控制台程序,其中有多个值映射到
dictionary
keyLookup
。我正在使用if命令,这些命令使用键输出一些
console.writeline=(“stuff”)
但它只有在值和键(在字典中)相同的情况下才起作用。我不知道这是为什么。我一直在玩弄
list
foreach
以及一些变量,试图找出我做错了什么,但即使它现在继续工作,它仍然不能按我想要的方式工作

另外,如果我的
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");
    }
}'
class类main类
{
公共静态字符串线;
静态字符串键;
公共静态void Main(字符串[]args)
{
while(true)
{
如果(行==null)
{Console.WriteLine(“输入”);}
单词检查器();
}
}
公共静态void WordChecker()
{
字符串inputString=Console.ReadLine();
inputString=inputString.ToLower();
字符串[]stripChars={“;”,“,”,“,”,“-”,“u”,“^”,“(“,”,“[”,“]),
“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“n”、“t”、“r”};
foreach(stripChars中的字符串字符)
{
inputString=inputString.Replace(字符“”);
}
//将空格拆分为字符串列表
List wordList=inputString.Split(“”).ToList();
//定义并删除停止字
string[]stopwords=新字符串[]{“and”,“the”,“she”,“for”,“this”,“you”,“but”};
foreach(stopwords中的字符串字)
{
//当单词列表中仍有stopword实例时,将其删除。
//如果我们不在这个问题上使用while循环,那么每次要删除的调用只会删除一个
//从我们的单词列表中删除stopword的实例,并且我们不能在
//整个字符串(与字符串中的单个单词相反)作为
//太不分青红皂白了(例如,去掉“and”会把“bandage”这样的词变成“bdage”!)
while(wordList.Contains(word))
{
删除(word);
}
}
//创建一个新的Dictionary对象
字典=新字典();
//在我们的单词列表中循环所有单词。。。
foreach(单词列表中的字符串单词)
{
//如果单词的长度至少是三个字母。。。
如果(字长>=3)
{
//…检查字典中是否已有该词。
if(字典.ContainsKey(单词))
{
//如果我们已经在字典中找到了这个词,则增加它出现的次数
字典[字]+;
}
其他的
{
//否则,如果是新词,则将其添加到字典中,初始计数为1
字典[字]=1;
}
}
List dicList=新列表();
dicList=dictionary.Keys.ToList();
Dictionary keyLookup=新建字典();
keyLookup[“嘿”]=“问候”;
keyLookup[“hi”]=“问候语”;
keyLookup[“问候语”]=“问候语”;
keyLookup[“math”]=“math”;
keyLookup[“计算”]=“数学”;
keyLookup[“等式”]=“数学”;
foundKey=keyLookup[word];
List keyList=新列表();
foreach(列表中的字符串关键字)
{
if(关键字==foundKey)
{keyList.Add(关键字);}
}
foreach(键列表中的字符串mKey)
{
if(mKey==“问候语”)
{问候();}
如果(mKey==“数学”)
{Math();}
}
}
}
公共静态无效数学()
{
Console.WriteLine(“你想让我学什么数学?”);
Console.WriteLine(“输入一个数字”);
字符串输入=Console.ReadLine();
十进制a=Convert.ToDecimal(输入);
WriteLine(“告诉我数学函数”);
字符串mFunction=Console.ReadLine();
Console.WriteLine(“告诉我另一个号码”);
字符串inputB=Console.ReadLine();
十进制b=转换为十进制(输入b);
Dictionary mathFunction=新字典();
mathFunction[“乘法”]=“乘法”;
mathFunction[“times”]=“multiply”;
数学函数[“x”]=“乘法”;
数学函数[“*”]=“乘法”;
mathFunction[“divide”]=“divide”;
数学函数[“/”]=“除法”;
数学函数[“减法”]=“减法”;
数学函数[“减法”]=“减法”;
数学函数[“-”]=“减法”;
mathFunction[“添加”]=“添加”;
数学函数[“+”]=“添加”;
mathFunction[“加”]=“添加”;
字符串foundKey=mathFunction[mFunction];
如果(foundKey==“添加”)
{
Console.WriteLine(a+b);
}
else if(foundKey==“减法”)
{
Console.WriteLine(a-b);
}
else if(foundKey==“乘法”)
{
Console.WriteLine(a*b);
}
else if(foundKey==“divide”)
{
控制台写入线(a/b);
}
其他的
{
Console.WriteLine(“不是数学”);
}
}
公共静态无效问候语()
{
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;
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(); }
}