无效的表达式'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; 
     } 
+6

从线删除分号'如果(长度== 3 &&利润 harpun 2013-02-24 15:57:39

+0

感谢您的帮助:) – 2013-02-24 16:01:42

+0

@AlanMcgilvray在您发现最有帮助的答案左边勾选大检查以表示已接受并回答问题;并奖励回答者。 – 2013-02-24 16:03:48

;终止的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; 
    } 
+0

这与地狱有什么关系?然而,这是一个经典的错字。 – rekire 2013-02-24 16:00:32

+0

谢谢你们我做了一个noob错误 – 2013-02-24 16:01:01

+0

Np。请记住,如果调试器给你非常荒谬的报告,或者更大的代码量被加下划线,则控制“;”和“()”。 – Stepo 2013-02-25 00:59:08

因为;它。它应该像

if (length == 3 && profit < 0) 
{ 
    //TODO: 
} 
else 
{ 
    //TODO: 
}