检查人员输入的姓名是否错误时出错

发布于 2024-09-18 14:24:26 字数 625 浏览 12 评论 0原文

我将如何检查错误。如果一个人输入了错误的姓名或拼写不正确,我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您需要保留正确姓名的列表或“词典”。

然后,您可以将文本与词典中的条目进行匹配。

代码将类似于以下内容:

HashSet<string> correctNames = ;// initialize the set with the names you want

private void button1_Click(object sender, EventArgs e)
{
    if (correctNames.Contains(textBox1.Text))
        Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
    else 
    {
       MessageBox.Show("The speling of the naem " + textBox1.Text + " was incorect", "Bad Spelling Error");
    }
}

您可能希望在实现中使用正确的拼写。

查看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:

HashSet<string> correctNames = ;// initialize the set with the names you want

private void button1_Click(object sender, EventArgs e)
{
    if (correctNames.Contains(textBox1.Text))
        Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
    else 
    {
       MessageBox.Show("The speling of the naem " + textBox1.Text + " was incorect", "Bad Spelling Error");
    }
}

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.

青柠芒果 2024-09-25 14:24:26

这将检查列表中的任何名称是否等于文本框中输入的名称:

List<string> nameList = new List<string>();

nameList.Add("Andrea");
nameList.Add("Brittany");
nameList.Add("Eric");

if (nameList.Contains(textBox1.Text))
{
    //Process name here.
}
else
{
    //Show messagebox here.
}

This will check if any name in the list equals the textBox entered name:

List<string> nameList = new List<string>();

nameList.Add("Andrea");
nameList.Add("Brittany");
nameList.Add("Eric");

if (nameList.Contains(textBox1.Text))
{
    //Process name here.
}
else
{
    //Show messagebox here.
}
马蹄踏│碎落叶 2024-09-25 14:24:26

将所有名称放入集合(例如列表或字典)中,然后使用 。 Contains() 方法。这应该提供一个更简洁的解决方案。

Throw all the names in a collection, like a List or Dictionary, then use the .Contains() method. That should provide a neater solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文