无效的表达式项';否则'

本文关键字:否则 表达式 无效 | 更新日期: 2023-09-27 18:00:55

我是C#的初学者,我只学习了几天。我正在尝试制作一个GW2交易站计算器,但我被卡住了。我试图检查一个字符串的长度是否等于3,如果它是-(像-21(,并且int的值是负数。我似乎很难理解这个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);<~该;

这是因为;。它应该像

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

if --> if (length == 3 && profit < 0) 之后删除;

这是完整的代码:

 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;
    }