我的嵌套if语句不起作用

时间:2015-05-12 21:21:20

标签: c# wpf xaml

我在C#WPF中进行编码。我的讲师说我的所有渔获都必须嵌套,我必须检查价格交易是否大于购买价格,也嵌套在渔获量内。我得到了捕获量,但当我在捕获量中添加if语句时,它不会起作用。有人可以告诉我我做错了什么吗?这是我的代码的相关部分。

        const decimal GST = .1m;
        decimal gstAmount, carPrice, tradeInPrice;
        private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                carPrice = decimal.Parse(vehiclePriceTextBox.Text);
                try
                {
                    tradeInPrice = decimal.Parse(lessTradeInTextBox.Text);

               try
               {

               if (decimal.Parse(lessTradeInTextBox.Text) > decimal.Parse(vehiclePriceTextBox.Text));
                   {  
                       MessageBox.Show("The trade-in price cannot be greater than the purchase price" + "Reason: " + theException.Message);
                   }
                   else
                   {
                    subAmountTextBox.Text = (decimal.Parse(vehiclePriceTextBox.Text) - decimal.Parse(lessTradeInTextBox.Text)).ToString();

                    gstAmount = (carPrice - (carPrice / (1 + GST)));
                    gstAmountTextBox.Text = gstAmount.ToString();

                    finalAmountTextBox.Text = (decimal.Parse(subAmountTextBox.Text) + decimal.Parse(gstAmountTextBox.Text)).ToString();
                   }

               }

                catch (FormatException theException)

                {
                    MessageBox.Show("Please enter a valid number: i.e. integer or decimal no. \n" + "Reason: " + theException.Message);
                    Keyboard.Focus(lessTradeInTextBox);
                    lessTradeInTextBox.SelectAll();
                }
            }
                catch (Exception theException)
                {
                    MessageBox.Show("Error, GeneralTransform Error. " + theException.Message);
                    Keyboard.Focus(vehiclePriceTextBox);
                    lessTradeInTextBox.SelectAll();
                }
            }
            catch (FormatException theException)
            {
                MessageBox.Show("Please enter a valid number: i.e. integer or decimal no.  \n" + "Reason: " + theException.Message);
                Keyboard.Focus(vehiclePriceTextBox);
                vehiclePriceTextBox.SelectAll();
            }
            catch (Exception theException)
            {
                MessageBox.Show("Error, GeneralTransform Error. " + theException.Message);
                Keyboard.Focus(vehiclePriceTextBox);
                vehiclePriceTextBox.SelectAll();
            }
        }

我收到这些错误

  

错误3;   预期E:\ 4C#B \ AssignmentPart1 \ Luke_Beauchamp_Part_1 \ Luke_Beauchamp_Part_1 \ MainWindow.xaml.cs 54 24 Luke_Beauchamp_Part_1   错误2无效的表达式术语   '否则' E:\ 4C#B \ AssignmentPart1 \ Luke_Beauchamp_Part_1 \ Luke_Beauchamp_Part_1 \ MainWindow.xaml.cs 54 20 Luke_Beauchamp_Part_1   错误5仅分配,调用,增量,减量,等待和新   对象表达式可以用作   语句E:\ 4C#B \ AssignmentPart1 \ Luke_Beauchamp_Part_1 \ Luke_Beauchamp_Part_1 \ MainWindow.xaml.cs 53 20 Luke_Beauchamp_Part_1   警告1可能误空   语句E:\ 4C#B \ AssignmentPart1 \ Luke_Beauchamp_Part_1 \ Luke_Beauchamp_Part_1 \ MainWindow.xaml.cs 50 101 Luke_Beauchamp_Part_1   错误4名称' theException'在当前不存在   上下文E:\ 4C#B \ AssignmentPart1 \ Luke_Beauchamp_Part_1 \ Luke_Beauchamp_Part_1 \ MainWindow.xaml.cs 52 118 Luke_Beauchamp_Part_1

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:6)

您的if语句后面有一个分号,它会立即结束。其他错误是以下else没有匹配if的结果。

答案 1 :(得分:3)

您不应该以分号结束if语句。只需删除它,一切都会好的!