C# 否则就不行了
我不知道我做错了什么!C# 否则就不行了,c#,C#,我不知道我做错了什么!else命令及其上方的括号似乎无法正确执行: public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (username_txtb.Text == username && password_txtb.Text == password); { MessageBox.Show
else
命令及其上方的括号似乎无法正确执行:
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("Sorry, you have used the wrong username and password. Please try again.");
}
}
应该是
else
删除分号,它将阻止正文执行
private void button1_Click(object sender, EventArgs e)
{
if (username_txtb.Text == username && password_txtb.Text == password) //; - remove
{
MessageBox.Show("You are now logged in!");
}
else //; - remove
{
MessageBox.Show("Sorry, you have used the wrong username and password. Please try again.");
}
}
应该是
else
删除分号,它将阻止正文执行
private void button1_Click(object sender, EventArgs e)
{
if (username_txtb.Text == username && password_txtb.Text == password) //; - remove
{
MessageBox.Show("You are now logged in!");
}
else //; - remove
{
MessageBox.Show("Sorry, you have used the wrong username and password. Please try again.");
}
}
请注意:
else ;
相当于:
else
{
}
else
{
}
{
//some code
}
else
{
}
// the conditional clauses are over,
// nothing special here except for an extra scope
// which is a valid construct (even though being useless here)
{
//some code
}
因此:
相当于:
else
{
}
else
{
}
{
//some code
}
else
{
}
// the conditional clauses are over,
// nothing special here except for an extra scope
// which is a valid construct (even though being useless here)
{
//some code
}
更清楚地说,这相当于:
else
{
}
else
{
}
{
//some code
}
else
{
}
// the conditional clauses are over,
// nothing special here except for an extra scope
// which is a valid construct (even though being useless here)
{
//some code
}
第二个块实际上与条件无关——它只是括号中的一个代码块,创建了一个无用的作用域,并且将始终被执行
如果,则在之后应用相同的规则 注意:
else ;
相当于:
else
{
}
else
{
}
{
//some code
}
else
{
}
// the conditional clauses are over,
// nothing special here except for an extra scope
// which is a valid construct (even though being useless here)
{
//some code
}
因此:
相当于:
else
{
}
else
{
}
{
//some code
}
else
{
}
// the conditional clauses are over,
// nothing special here except for an extra scope
// which is a valid construct (even though being useless here)
{
//some code
}
更清楚地说,这相当于:
else
{
}
else
{
}
{
//some code
}
else
{
}
// the conditional clauses are over,
// nothing special here except for an extra scope
// which is a valid construct (even though being useless here)
{
//some code
}
第二个块实际上与条件无关——它只是括号中的一个代码块,创建了一个无用的作用域,并且将始终被执行
如果
,则在之后应用相同的规则 显示的代码导致语法错误,应为:
无效的表达式术语“else”
语法错误是由if
后面的分号引起的(在else
后面的分号在运行程序时会导致“意外行为”,同样的原则也适用)
相当于
if (..)
/* empty statement - no code run when if is true! */ ;
/* arbitrary code in a block *not associated* with the `if` */
{
..
}
else ..
在这种情况下,“任意代码块”结束if语句
语法构造
要修复语法错误(和语义问题),请删除if
和else
语句后的分号:
if (..) /* no semicolon */
{
MessageBox.Show("..");
}
else /* no semicolon */
{
MessageBox.Show("..");
}
显示的代码导致语法错误,应为:
无效的表达式术语“else”
语法错误是由if
后面的分号引起的(在else
后面的分号在运行程序时会导致“意外行为”,同样的原则也适用)
相当于
if (..)
/* empty statement - no code run when if is true! */ ;
/* arbitrary code in a block *not associated* with the `if` */
{
..
}
else ..
在这种情况下,“任意代码块”结束if语句
语法构造
要修复语法错误(和语义问题),请删除if
和else
语句后的分号:
if (..) /* no semicolon */
{
MessageBox.Show("..");
}
else /* no semicolon */
{
MessageBox.Show("..");
}
删除分号吉尼纳的经验法则:关键字始终有效,这始终是开发人员的错误。删除分号吉尼纳的经验法则:关键字始终有效,这始终是开发人员的错误。当我这样做,输入正确的用户名和密码时,它会告诉我正确,但它也会告诉我转到“其他”并告诉我,“您输入的用户名或密码不正确!“@user3002617你到底做了什么?我没有给出问题的明确答案,而是留给你去解决。这应该不会很难,因为在其他答案中,你会免费提供解决方案。你描述的行为表明,你没有遵循所有提示。当我这样做时,我会输入正确的用户名和密码它告诉我是正确的,但它也告诉我转到else并告诉我,“您输入了错误的用户名或密码!“@user3002617你到底做了什么?我没有给出问题的明确答案,而是留给你去解决。这应该不会很难,因为在其他答案中,你会免费提供解决方案。你描述的行为表明,你没有遵循所有提示。谢谢,去掉分号并修复顶部!比k谢谢。@user3002617很酷。记住,在删除两个额外分号之前,它不会被“修复”。在else
后面的分号将使它看起来好像else“总是运行一样“-不会,因为块与else
语句没有关联-即使它不会像前一种情况那样导致语法错误。谢谢,去掉分号并修复顶部部分!”!谢谢。@user3002617很酷。请记住,在删除两个额外分号之前,它不会被“修复”。else
后面的分号将使它看起来好像else“始终运行”-它不会,因为块与else
语句没有关联-即使它不会像前一种情况那样导致语法错误。