我希望能够检查字符串是否包含列表中的所有值;因此,只有当您在回答中包含列表中的所有‘关键字’时,它才会给出一个“正确的答案”。这是我累了一半失败的东西;(不检查所有数组,只接受一个数组)。代码我累了:
foreach (String s in KeyWords)
{
if (textBox1.Text.Contains(s))
{
correct += 1;
MessageBox.Show("Correct!");
LoadUp();
}
else
{
incorrect += 1;
MessageBox.Show("Incorrect.");
LoadUp();
}
}基本上,我想做的是:
问:心理学的定义是什么?
研究,心理过程,行为,人类
答:心理学是人类的心理过程( study of humans behaviour of )。
现在,如果并且只有当上面的答案包含所有关键字时,我的代码才会接受答案。我希望我对此已经很清楚了。
编辑:谢谢大家的帮助。所有的答案都被投票通过了,我感谢大家的快速回答。我选了一个可以很容易地适应任何代码的答案。:)
发布于 2013-02-06 11:50:32
使用LINQ:
// case insensitive check to eliminate user input case differences
var invariantText = textBox1.Text.ToUpperInvariant();
bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant()));发布于 2013-02-06 11:53:20
这应有助于:
string text = "Psychology is the study of mental process and behaviour of humans";
bool containsAllKeyWords = KeyWords.All(text.Contains);发布于 2013-02-06 11:50:07
您可以使用以下LINQ方法:
if(Keywords.All(k => textBox1.Text.Contains(k))) {
correct += 1;
MessageBox.Show("Correct");
} else {
incorrect -= 1;
MessageBox.Show("Incorrect");
}当函数返回列表中所有项的true时,All方法将返回true。
https://stackoverflow.com/questions/14728294
复制相似问题