if else语句无法退出/中断

时间:2017-08-07 05:15:12

标签: c# winforms loops

当项目数量不足时,我的if else语句不会中断。它显示它的数量不足但仍然继续,因此其数量变为负数。

例如。产品A在数据库中的数量为20。在销售方面,我试图销售25个。它显示数量不足"但仍然减去数量,所以数量变为-5。我希望它停止时它说'#34;数量不足"。

这是我的代码

   private void btnAddcart_Click(object sender, EventArgs e)
   {
        if (!validateProduct())
        {
            return;
        }
        else
        {
            ShowMyDialogBox();
        }
        if (!alreadyincart())
        {
            return;
        }
        else
        {

            int str, qty;
            str = Convert.ToInt32(storeqty.Text);
            qty = Convert.ToInt32(quantity.Text);
            temporaryquantity.Text = str.ToString();

            if (str < qty || str == 0)
            {
                MessageBox.Show("Insufficient Stock", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);

                //its supposed to stop here but its still subtracting the 
                // quantity from the product
            }
            else
            {
                qty = Convert.ToInt32(quantity.Text);
                unitprice = Convert.ToDouble(dgvPOSproduct.CurrentRow.Cells[6].Value.ToString());
                totalprice = qty * unitprice;
                unittotal.Text = totalprice.ToString("0.00");

                addData
                    (
                    dgvPOSproduct.CurrentRow.Cells[0].Value.ToString(), //prod id
                    dgvPOSproduct.CurrentRow.Cells[1].Value.ToString(), //brand
                    dgvPOSproduct.CurrentRow.Cells[4].Value.ToString(), //dosage
                    dgvPOSproduct.CurrentRow.Cells[6].Value.ToString(), //qty
                    quantity.Text,
                    unittotal.Text,
                    dgvPOSproduct.CurrentRow.Cells[7].Value.ToString(),
                    dgvPOSproduct.CurrentRow.Cells[8].Value.ToString()
                    );
            }

            int dgvPOSquantity = Convert.ToInt32(dgvPOSproduct.CurrentRow.Cells[5].Value.ToString());
            int dgvnewquantity;
            dgvnewquantity = dgvPOSquantity - qty;
            dgvPOSproduct.CurrentRow.Cells[5].Value = dgvnewquantity;

            discountremoveitem();

        }

    }

1 个答案:

答案 0 :(得分:2)

这只是因为你有If语句之外的逻辑。

意味着它运行这一行 -

if (str < qty || str == 0)

然后匹配条件并跳出if语句,点击下一行

int dgvPOSquantity

哪个是减法。

将减法代码移到else语句中或更改逻辑流程。

例如:

if (str < qty || str == 0)
        {
            MessageBox.Show("Insufficient Stock", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
//This statement is getting hit and exits the IF Statement
        }
        else
        {
            qty = Convert.ToInt32(quantity.Text);
            unitprice = Convert.ToDouble(dgvPOSproduct.CurrentRow.Cells[6].Value.ToString());
            totalprice = qty * unitprice;
            unittotal.Text = totalprice.ToString("0.00");

            addData
                (
                dgvPOSproduct.CurrentRow.Cells[0].Value.ToString(), //prod id
                dgvPOSproduct.CurrentRow.Cells[1].Value.ToString(), //brand
                dgvPOSproduct.CurrentRow.Cells[4].Value.ToString(), //dosage
                dgvPOSproduct.CurrentRow.Cells[6].Value.ToString(), //qty
                quantity.Text,
                unittotal.Text,
                dgvPOSproduct.CurrentRow.Cells[7].Value.ToString(),
                dgvPOSproduct.CurrentRow.Cells[8].Value.ToString()
                );
        }

//But then carries on from here, which does the subtraction. 
//You need to either move this code snippet into the else statement, or change the flow.

        int dgvPOSquantity = Convert.ToInt32(dgvPOSproduct.CurrentRow.Cells[5].Value.ToString());
        int dgvnewquantity;
        dgvnewquantity = dgvPOSquantity - qty;
        dgvPOSproduct.CurrentRow.Cells[5].Value = dgvnewquantity;

        discountremoveitem();

    }
相关问题