所以这里的一切似乎都是正确的我想知道为什么'其他'会通过。当我输入正确的用户名和密码时,告诉我“您现在已登录!”但它告诉我“你输入了错误的用户名或密码!” (我正在使用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!");
}
}
}
}
答案 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!");
}