为什么我的服务费总是显示 0 而不是 206?

Why do my service charges keep saying 0 instead of 206?

我无法找出为什么我的代码没有在服务收费标签中添加润滑油、冲洗和杂项复选框。我将它们添加到计算按钮中,它们都已声明,但我仍然以 0 结束。例如,如果检查换油、变速箱冲洗和消声器,我应该以 206 结束。有谁知道为什么我仍然以0结束?谢谢!

namespace Semester_Projectt
{

    public partial class semesterProject : Form
    {
        double oilChange;
        double lubeJob;
        double radiatorFlush;
        double transmissionFlush;
        double inspection;
        double muffler;
        double tireRotation;
        const double taxRate = 0.06;

        public semesterProject()
        {
            InitializeComponent();
        }
        private double OilLubeCharges(double oilChange, double lubeJob)
        {
            return oilChange + lubeJob;
        }
        private double FlushCharges(double radiatorFlush, double transmissionFlush)
        {
            return radiatorFlush + transmissionFlush;
        }
        private double MiscCharges(double inspection, double muffler, double tireRotation)
        {
            return inspection + muffler + tireRotation;
        }
        private double OtherCharges(double parts, double labor)
        {
            return parts + labor;
        }
        private double TaxCharges(double parts)
        {
            if (parts != 0)
            {
                return (0.06 * parts);
            }
            else
                return 0;
        }
        private double TotalCharges(double oilLube, double flush, double misc, double other, double tax)
        {
            return oilLube + flush + misc + other + tax;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            ClearOilLube();
            ClearFlushes();
            ClearMisc();
            ClearOther();
            ClearFees();
        }
        private void ClearOilLube()
        {
            if(oilCheckBox.Checked == true)
            {
                oilCheckBox.Checked = false;
            }
            if(lubeCheckBox.Checked == true)
            {
                lubeCheckBox.Checked = false;
            }
        }
        private void ClearFlushes()
        {
            if(radiatorCheckBox.Checked == true)
            {
                radiatorCheckBox.Checked = false;
            }
            if(transmissionCheckBox.Checked == true)
            {
                transmissionCheckBox.Checked = false;
            }
        }
        private void ClearMisc()
        {
            if(inspectionCheckBox.Checked == true)
            {
                inspectionCheckBox.Checked = false;
            }
            if(mufflerCheckBox.Checked == true)
            {
                mufflerCheckBox.Checked = false;
            }
            if(tireCheckBox.Checked == true)
            {
                tireCheckBox.Checked = false;
            }
        }
        private void ClearOther()
        {
            partsTextBox.Text = "";
            laborTextBox.Text = "";
        }
        private void ClearFees()
        {
            serviceChargeLabel.Text = "";
            additionalPartsLabel.Text = "";
            additionalLaborLabel.Text = "";
            taxLabel.Text = "";
            totalFeesLabel.Text = "";
        }
        private void calculateButton_Click(object sender, EventArgs e)
        {
            double parts = double.Parse(partsTextBox.Text);
            double labor = double.Parse(laborTextBox.Text);
            double oilLube = OilLubeCharges(oilChange, lubeJob);
            double flush = FlushCharges(radiatorFlush, transmissionFlush);
            double misc = MiscCharges(inspection, muffler, tireRotation);
            double other = OtherCharges(parts, labor);
            double tax = TaxCharges(parts);
            double total = TotalCharges(oilLube, flush, misc, other, tax);
            double services = oilLube + flush + misc;

            serviceChargeLabel.Text = services.ToString("c");
            additionalPartsLabel.Text = parts.ToString("c");
            additionalLaborLabel.Text = labor.ToString("c");
            taxLabel.Text = tax.ToString("c");
            totalFeesLabel.Text = total.ToString("c");

            try
            {
                parts = double.Parse(partsTextBox.Text);

                try
                {
                    labor = double.Parse(laborTextBox.Text);
                }
                catch
                {
                    MessageBox.Show("Entries must be numeric", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    partsTextBox.Focus();
                    partsTextBox.SelectAll();
                }
            }
            catch
            {
                MessageBox.Show("Entries must be numeric", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                laborTextBox.Focus();
                laborTextBox.SelectAll();
            }

            if (oilCheckBox.Checked == true)
            {
                oilChange = 26.00;
            }
            if (lubeCheckBox.Checked == true)
            {
                lubeJob = 18.00;
            }
            if (radiatorCheckBox.Checked == true)
            {
                radiatorFlush = 30.00;
            }
            if (transmissionCheckBox.Checked == true)
            {
                transmissionFlush = 80.00;
            }
            if (inspectionCheckBox.Checked == true)
            {
                inspection = 15.00;
            }
            if (mufflerCheckBox.Checked == true)
            {
                muffler = 100.00;
            }
            if (tireCheckBox.Checked == true)
            {
                tireRotation = 20.00;
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

使用调试器单步执行您的代码,我想您会发现当您将所有成本变量(即 oilChange)加在一起时它们都是 0。

显示 MessageBoxes 后,如果复选框被选中,您有几个 IF 语句设置可变成本。看起来那些 IF 语句应该移到过程的顶部。