在下面的代码中,Visual Studio会在其他单词上添加错误消息。特定错误显示为“Unexpected'else'”。我做错了什么?
decimal AmountToAccrue;
string BillingDescription;
if (PromoPeriodEnd >= day)
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
else
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
答案 0 :(得分:17)
当你有多行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";
答案 1 :(得分:4)
你需要大括号,因为你有if和else主体的复合语句。
if (PromoPeriodEnd >= day)
{
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else
{
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}
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";
这实际上是为什么许多编码标准建议总是使用带有控制语句的大括号,因此如果稍后将其从一个语句主体更改为复合语句主体,则不会出现此类错误。
答案 2 :(得分:1)
似乎你缺少一些括号:
if (PromoPeriodEnd >= day)
{
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else
{
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}
在C#中,与大多数(所有?)C语言一样,那些大括号定义了代码块。这些代码块非常重要 - 它们还定义了变量范围(参见Eric的评论),所以一定要了解它们。
答案 3 :(得分:1)
当你不使用大括号时,它只识别第一个语句......所以你的代码就变成了这个
if (PromoPeriodEnd >= day) {
AmountToAccrue = 0;
}
BillingDescription = "Subscription 30-day Promotional Period";
else {
AmountToAccrue = subscription.Amount * ProratedPercentDue;
}
BillingDescription = "Subscription Fee";
当然,这不是它的期望,即使是新手也很愚蠢:)
答案 4 :(得分:0)
对于多行,你应该使用括号
if (PromoPeriodEnd >= day){
AmountToAccrue = 0;
BillingDescription = "Subscription 30-day Promotional Period";
}
else {
AmountToAccrue = subscription.Amount * ProratedPercentDue;
BillingDescription = "Subscription Fee";
}