是什么导致了这种“;意外的';其他'";错误
本文关键字:其他 quot 错误 意外 是什么 | 更新日期: 2023-09-27 18:16:19
在下面的代码中,Visual Studio在单词else上添加了一条错误消息。特定错误为"意外的'else'"。我做错了什么?
decimal AmountToAccrue;
string BillingDescription;
if (PromoPeriodEnd >= day)
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
else
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
当您有多行if
和else
时,必须使用大括号:
if (PromoPeriodEnd >= day)
{
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else
{
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}
附加信息:
如果没有大括号,则只考虑范围内的下一个语句,而不是行。
确定:
if (PromoPeriodEnd >= day)
AmountToAccrue = 0;
else
AmountToAccrue = subscription.Amount * ProratedPercentDue;
不正常:
if (PromoPeriodEnd >= day)
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
else
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
编译器会看到"不正常"的例子,如下所示:
//Begin if statement
if (PromoPeriodEnd >= day)
AmountToAccrue = 0;
//End if statement
//Set BillingDescription (outside of the else scope)
BillingDescription = "Subscription 30-day Promotional Period";
//Begin else statement - ERROR! Where is the matching if?
else
AmountToAccrue = subscription.Amount * ProratedPercentDue;
//Set BillingDescription(outside of the else scope) - error above - never reached
BillingDescription = "Subscription Fee";
需要大括号,因为if和else体有一个复合语句。
if (PromoPeriodEnd >= day)
{
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else
{
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}
如果条件为true,则if
条件之后的下一个语句将被执行。这可以是一条语句,也可以是一个复合语句(一系列用大括号括起来的语句(。
因为你没有花括号,它将其解释为:
if (PromoPeriodEnd >= day)
AmountToAccrue = 0; // This is the body of the if
// this is outside of the if
BillingDescription = "Subscription 30-day Promotional Period";
// this token makes no sense here because it is not after the if body.
else
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
这就是为什么许多编码标准建议始终在控制语句中使用大括号的原因,这样,如果以后将其从一个语句体更改为复合语句体,就不会出现这种类型的错误。
如果不使用大括号,它只识别第一个语句。。。所以你的代码变成了这个
if (PromoPeriodEnd >= day) {
AmountToAccrue = 0;
}
BillingDescription = "Subscription 30-day Promotional Period";
else {
AmountToAccrue = subscription.Amount * ProratedPercentDue;
}
BillingDescription = "Subscription Fee";
这当然不是它所期望的,即使是新手也很傻:(
似乎缺少一些括号:
if (PromoPeriodEnd >= day)
{
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else
{
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}
在C#中,就像在大多数(所有?(C语言中一样,那些花括号定义了代码块。这些代码块非常重要——它们还定义了变量scope(请参阅Eric的评论(,因此请确保您理解它们。
对于多行,应该使用括号
if (PromoPeriodEnd >= day){
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else {
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}