如何在用户输入0时获得不是错误的输出

时间:2017-02-14 15:28:30

标签: c# visual-studio

这是我的第一个真正的计划,我知道它不漂亮,但我很自豪我做到了。如果用户输入0作为利率,则不会为每月付款提供价值。我该如何解决这个问题?

    private void buttonPayment_Click(object sender, EventArgs e)
    {

        double sellPrice = 0;
        double stateTax = 0;
        double totalFinanced = 0;
        double amtTax = 0;
        double docFee = 179;
        double tagFees = 0;
        double tradeAllowance = 0;
        double interestRate = 0;
        double taxableAmount = 0;
        double rebate = 0;
        double cashDown = 0;

        // Used For Payments
        double term = 0;
        double monthlyRate = 0;
        double monthlyPayment = 0;
        double topNumber1 = 0;
        double topNumber = 0;
        double bottomNumber = 0;
        double topNumber2 = 0;
        double totalInterest = 0;
        double interestCharge = 0;

        sellPrice = Double.Parse(textBoxSellPrice.Text);
        tradeAllowance = Double.Parse(textBoxTradeAllowance.Text);
        tagFees = Double.Parse(textBoxTagFees.Text);
        rebate = Double.Parse(textBoxRebate.Text);
        interestRate = Double.Parse(textBoxRate.Text);
        term = Double.Parse(textBoxTerm.Text);
        cashDown = Double.Parse(textBoxCashDown.Text);


        interestRate = interestRate * .01;
        monthlyRate = interestRate / 12;


        if (comboBoxState.Text.Contains("NJ"))
        {
            stateTax = .06875;
        }

        if (comboBoxState.Text.Contains("DE"))
        {
            stateTax = .0425;
        }

        if (comboBoxState.Text.Contains("PA"))
        {
            stateTax = .06;
        }


        //Calculatations

        taxableAmount = sellPrice - tradeAllowance;
        amtTax = taxableAmount * stateTax;
        totalFinanced = (amtTax + taxableAmount + docFee + tagFees - rebate - cashDown);

        //Calculates Payments

        topNumber1 = 1 + monthlyRate;
        topNumber2 = Math.Pow(topNumber1, term);
        topNumber = monthlyRate * topNumber2;
        bottomNumber = topNumber2 - 1;
        totalInterest = topNumber / bottomNumber;

        monthlyPayment = totalFinanced * totalInterest;
        interestCharge = (monthlyPayment * term) - totalFinanced;

        // Prints to the labels

        labelTax.Text = amtTax.ToString(format: "0.00");
        labelAmtFinanced.Text = totalFinanced.ToString(format: "0.00");
        labelDocFee.Text = docFee.ToString(format: "0.00");
        labelTagFees.Text = tagFees.ToString(format: "0.00");
        labelMonthlyPayment.Text = monthlyPayment.ToString(format: "0.00");
        labelInterestCharge.Text = interestCharge.ToString(format: "0.00");
    }

    private void buttonClear_Click(object sender, EventArgs e)
    {
        ClearAllText(this);
        ClearAllCombo(this);
        ClearAllmasked(this);
        labelTax.Text = "---";
        labelDocFee.Text = "---";
        labelTagFees.Text = "---";
        labelAmtFinanced.Text = "---";
        labelMonthlyPayment.Text = "---";
        labelInterestCharge.Text = "---";
    }

我知道它的草率:(

再次感谢!

0 个答案:

没有答案