2013-02-24 189 views
0

我是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 &&利潤<0);'和代碼將編譯。 – harpun 2013-02-24 15:57:39

+0

感謝您的幫助:) – 2013-02-24 16:01:42

+0

@AlanMcgilvray在您發現最有幫助的答案左邊勾選大檢查以表示已接受並回答問題;並獎勵回答者。 – 2013-02-24 16:03:48

回答

4

;終止的if()語句,它後else說法變成了「懸空」 else語句,這是違法的。

卸下後; if (length == 3 && profit < 0); <〜這;

+0

謝謝你解釋爲什麼:) – 2013-02-24 16:21:35

1

刪除;是完整的代碼:

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

3

因爲;它。它應該像

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