无效的表达式'else'
问题描述:
我是C#的初学者 - 我只学了几天。我试图制作一个GW2交易后期计算器,但我被卡住了。我试图检查一个字符串的长度是否等于3,以防其-
(如-21
),并且int的值是负值。我似乎无法看到这个else
声明中哪里出错。后if --> if (length == 3 && profit < 0)
无效的表达式'else'
这里
sellPrice = sellPrice * 0.85;
profit = (int)sellPrice - buyPrice;
String copperString;
copperString = profit.ToString();
int length = copperString.Length;
if (length == 3 && profit < 0);
{
copperString = copperString.Substring(Math.Max(0, copperString.Length - 3));
this.textBox3.Text = copperString;
}
else
{
copperString = copperString.Substring(Math.Max(0, copperString.Length - 2));
this.textBox3.Text = copperString;
}
答
;
终止的if()
语句,它后else
说法变成了“悬空” else语句,这是违法的。
卸下后;
if (length == 3 && profit < 0);
<〜这;
+0
谢谢你解释为什么:) – 2013-02-24 16:21:35
答
删除;
是完整的代码:
sellPrice = sellPrice * 0.85;
profit = (int)sellPrice - buyPrice;
String copperString;
copperString = profit.ToString();
int length = copperString.Length;
if (length == 3 && profit < 0)
{
copperString = copperString.Substring(Math.Max(0, copperString.Length - 3));
this.textBox3.Text = copperString;
}
else
{
copperString = copperString.Substring(Math.Max(0, copperString.Length - 2));
this.textBox3.Text = copperString;
}
答
因为;
它。它应该像
if (length == 3 && profit < 0)
{
//TODO:
}
else
{
//TODO:
}
从线删除分号'如果(长度== 3 &&利润 harpun 2013-02-24 15:57:39
感谢您的帮助:) – 2013-02-24 16:01:42
@AlanMcgilvray在您发现最有帮助的答案左边勾选大检查以表示已接受并回答问题;并奖励回答者。 – 2013-02-24 16:03:48