我希望能够检查字符串是否包含列表中保存的所有值; 因此,如果您在答案中列出了所有"关键词",它只会给您"正确答案".继承人我累了哪一半失败了;(不检查所有阵列,只接受一个).代码我累了:
foreach (String s in KeyWords)
{
if (textBox1.Text.Contains(s))
{
correct += 1;
MessageBox.Show("Correct!");
LoadUp();
}
else
{
incorrect += 1;
MessageBox.Show("Incorrect.");
LoadUp();
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我想做的是:
问题:心理学的定义是什么?
关于arraylist的关键词:研究,心理过程,行为,人类
答:心理学是研究的心理过程和行为的人
现在,如果以上答案包含所有关键词,我的代码将接受答案.我希望我对此很清楚.
编辑:谢谢大家的帮助.所有答案都已经投票,我感谢大家的快速解答.我投了答案,可以很容易地适应任何代码.:)
sll*_*sll 15
使用LINQ:
// case insensitive check to eliminate user input case differences
var invariantText = textBox1.Text.ToUpperInvariant();
bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant()));
Run Code Online (Sandbox Code Playgroud)
这应该有助于:
string text = "Psychology is the study of mental process and behaviour of humans";
bool containsAllKeyWords = KeyWords.All(text.Contains);
Run Code Online (Sandbox Code Playgroud)