在做了很多教程之后,我正在制作我的第一个程序。它或多或少是一个控制台程序,我在其中输入句子并执行操作。
我让它在一个关键字系统上工作,它摆脱了少于 3 个字符的单词并将其他所有内容添加到动态生成的字典中。
到目前为止,我一直在使用ContainsKey()
它一次检查一个键。我真正想做的是使用另一个字典,或者一个列表,或者一个数组来保存一堆键来遍历字典。
例如,如果我有一个问候列表:
{ "hi", "hey", "hello" }
我希望程序为每一个输出相同的文本。必须有一种更好的方法,而不是if
为我要检查字典的每个单词单独声明。
我已经对这个主题进行了一些网络搜索,并且我一直在阅读一些叫做 an 的东西,IEqualityComparer
但老实说,这听起来超出了我的能力范围。还有其他方法可以做到这一点,还是我应该直接加入IEqualityComparer
并尝试用我不理解的东西来糊弄我的方式?
class MainClass
{
static string Line;
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;
}
}
if (dictionary.ContainsKey ("math")) {
Console.WriteLine ("What do you want me to math?");
Math ();
}
if(dictionary.ContainsKey("fruit"))
{Console.WriteLine("You said something about fruit");}
}
}
public static void 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);
if (mFunction == "add")
{
Console.WriteLine (a + b);
}
else if (mFunction == "subtract")
{
Console.WriteLine (a - b);
}
else if (mFunction == "multiply")
{
Console.WriteLine (a * b);
}
else if (mFunction == "divide")
{
Console.WriteLine (a / b);
}
else
{
Console.WriteLine ("not a math");
}
}
public static void Greetings()
{
}
}
注意:我从网上找到的一个示例中获得了动态词典和单词解析器,并对其进行了一些修改以满足我的需要。我不会自己想出它,但我确实觉得我理解其中的代码。