检查人员输入的姓名是否错误时出错
我将如何检查错误。如果一个人输入了错误的姓名或拼写不正确,我希望 messagebox.show 显示一条消息,指出“姓名或拼写错误”
private void button1_Click(object sender, EventArgs e)
{
String Andrea;
String Brittany;
String Eric;
if (textBox1.Text == ("Andrea"))
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
if (textBox1.Text == ("Brittany"))
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
if (textBox1.Text ==("Eric"))
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
{
}
}
How would I check for errors. If a person enters an incorrect name or doesnt spell it correctly I would like for a messagebox.show to display a message stating "Incorrect name or spelling"
private void button1_Click(object sender, EventArgs e)
{
String Andrea;
String Brittany;
String Eric;
if (textBox1.Text == ("Andrea"))
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
if (textBox1.Text == ("Brittany"))
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
if (textBox1.Text ==("Eric"))
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要保留正确姓名的列表或“词典”。
然后,您可以将文本与词典中的条目进行匹配。
代码将类似于以下内容:
您可能希望在实现中使用正确的拼写。
查看
HashSet
的文档,以更好地了解如何使用它。You will need to keep a list or 'dictionary' of correct names.
Then, you can match the text against the entries in the dictionary.
Code would look similar to the following:
You probably want to use correct spelling in your implementation.
Have a look at the documentation for
HashSet
to get a better idea of how to use it.这将检查列表中的任何名称是否等于文本框中输入的名称:
This will check if any name in the list equals the textBox entered name:
将所有名称放入集合(例如列表或字典)中,然后使用 。 Contains() 方法。这应该提供一个更简洁的解决方案。
Throw all the names in a collection, like a List or Dictionary, then use the .Contains() method. That should provide a neater solution.