当我不想要它时,我的消息框会显示

时间:2013-11-17 22:26:02

标签: c#

所以这里的一切似乎都是正确的我想知道为什么'其他'会通过。当我输入正确的用户名和密码时,告诉我“您现在已登录!”但它告诉我“你输入了错误的用户名或密码!” (我正在使用C#)(新程序员警报!)

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (username_txtb.Text == username && password_txtb.Text == password)
        {
            MessageBox.Show("You are now logged in!");
        }
        else
        {
        }
        {
            MessageBox.Show("You have entered an incorrect username or password!");
        }
    }

}

}

4 个答案:

答案 0 :(得分:3)

        else
        {//remove
        }//this part
        {
            MessageBox.Show("You have entered an incorrect username or password!");
        }

答案 1 :(得分:1)

你有一组额外的花括号,因此显示第二个消息框的代码总是被执行:

试试这个:

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    if (username_txtb.Text == username && password_txtb.Text == password)
    {
        MessageBox.Show("You are now logged in!");
    }
    else
    {
        MessageBox.Show("You have entered an incorrect username or password!");
    }
}

}

答案 2 :(得分:1)

由于大括号,else分支为空。它应该删除。

更改以下

else
        {
        }
        {
            MessageBox.Show("You have entered an incorrect username or password!");
        }

 else
        {
            MessageBox.Show("You have entered an incorrect username or password!");
        }

答案 3 :(得分:1)

您已放置多个括号,打开其他并立即关闭它。像这样重写你的代码

   if (username_txtb.Text == username && password_txtb.Text == password)
    {
        MessageBox.Show("You are now logged in!");
    }
    else
    {
        MessageBox.Show("You have entered an incorrect username or password!");
    }