检查文本框是否包含一个不起作用的词
checking if textbox contains a word not working
它似乎只 detect/check 列表中的第一个单词。
private bool ContentCheck()
{
List<string> FilteredWords = new List<string>()
{
"duck",
"donkey",
"horse",
"goat",
"dog",
"cat", //list of censored words
"lion",
"tiger",
"bear",
"crocodile",
"eel",
};
foreach (var e in FilteredWords)
{
if (memoEdit1.Text.Contains(e))
{
return true;
}
else
{
return false;
}
}
return false;
}
private void button_click (object sender, EventArgs e)
{
if (ContentCheck() == false)
{
//do something
}
else if (ContentCheck() == true)
{
MessageBox.Show("Error: Offensive word.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
foreach
块中的 if
语句中的两种情况都会导致 return
。想一想,程序将迭代列表中的第一项,如果它是脏话它会 return,如果不是它会 也 return .这两个都将退出代码,因此永远不会迭代下一个项目。
要解决此问题,您需要更改
foreach (var e in FilteredWords)
{
if (memoEdit1.Text.Contains(e))
{
return true;
}
else
{
return false;
}
}
return false;
到
foreach (var e in FilteredWords)
{
if (memoEdit1.Text.Contains(e))
{
return true;
}
}
return false;
它似乎只 detect/check 列表中的第一个单词。
private bool ContentCheck()
{
List<string> FilteredWords = new List<string>()
{
"duck",
"donkey",
"horse",
"goat",
"dog",
"cat", //list of censored words
"lion",
"tiger",
"bear",
"crocodile",
"eel",
};
foreach (var e in FilteredWords)
{
if (memoEdit1.Text.Contains(e))
{
return true;
}
else
{
return false;
}
}
return false;
}
private void button_click (object sender, EventArgs e)
{
if (ContentCheck() == false)
{
//do something
}
else if (ContentCheck() == true)
{
MessageBox.Show("Error: Offensive word.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
foreach
块中的 if
语句中的两种情况都会导致 return
。想一想,程序将迭代列表中的第一项,如果它是脏话它会 return,如果不是它会 也 return .这两个都将退出代码,因此永远不会迭代下一个项目。
要解决此问题,您需要更改
foreach (var e in FilteredWords)
{
if (memoEdit1.Text.Contains(e))
{
return true;
}
else
{
return false;
}
}
return false;
到
foreach (var e in FilteredWords)
{
if (memoEdit1.Text.Contains(e))
{
return true;
}
}
return false;