C#无限循环帮助基本编程

时间:2014-07-28 05:09:15

标签: c# infinite-loop tryparse

我是第二季度的c#编程,我正在开发POS应用程序。我创建了我的Windows窗体,并且在分配的第一周完成了我的基本代码。现在我必须" idiot-proof"我的代码确保只能输入正确的数据。这就是我到目前为止所拥有的:

private void btnAddItem_Click(object sender, EventArgs e)
{
    //Declare variables
    double dblSalesTax = 0, dblPrice, dblTax, dblSalesPrice;
    string strItem, strTaxAdded;
    int intQuantity;
    bool diffTest = false;

    //Process user input

    while (!diffTest)
    {
        diffTest = double.TryParse(txtSalesTax.Text, out dblSalesTax);
    }   

    while (dblSalesTax < 0 || dblSalesTax > 25)
    {
        MessageBox.Show("Please enter a valid tax.");
        txtSalesTax.Clear();

        diffTest = false;
    }

    intQuantity = Convert.ToInt16(txtQuantity.Text);
    dblPrice = Convert.ToDouble(txtPrice.Text);
    dblSalesPrice = dblPrice * intQuantity;
    strItem = cbxItem.Text;
    intQuantity = Convert.ToInt16(txtQuantity.Text);

    dblSubtotal += dblSalesPrice;

    if (chkTaxExempt.Checked)
    {
        dblTax = 0;
        strTaxAdded = "";
    }
    else
    {
        dblTax = dblSalesPrice * dblSalesTax;
        strTaxAdded = "*";
    }

    dblTaxTotal += dblTax;

    lbxTally.Items.Add(strItem + ", " + dblSalesPrice.ToString("C") + strTaxAdded);

    //Reset Form

    txtPrice.Clear();
    txtQuantity.Clear();
    chkTaxExempt.Checked = false;
    cbxItem.Focus();
}


private void btnEndSale_Click(object sender, EventArgs e)
{
    dblGrandTotal = dblSubtotal + dblTaxTotal;
    lbxTally.Items.Add("");
    lbxTally.Items.Add("");
    lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
    lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
    lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}

private void btnPay_Click(object sender, EventArgs e)
{
    double dblPay, dblChange;

    dblPay = Convert.ToDouble(txtPay.Text);
    dblChange = dblPay - dblGrandTotal;
    lbxTally.Items.Add("");
    lbxTally.Items.Add("Amount Paid: " + dblPay.ToString("C"));
    lbxTally.Items.Add("Change: " + dblChange.ToString("C"));
}

预先声明变量,diffTest初始化为false。 分配是为了确保输入的销售税在0到25之间,并且他们无法输入单词或其他任何内容。我认为我做得对,但是当我运行它时,我的消息框上有一个无限循环,我无法弄清楚如何正确地离开它(进入休息只是让我出去但保留输入)。我已经谷歌了解了我的内容,但还没有找到解决方案,但我觉得这样,因为我的代码会自动重复使用文本框中的内容(我可能会非常错误) !)。一旦我得到这个,我就必须&#34;白痴证明&#34;我的其他投入,但我还没有尝试,因为我仍然坚持第一个。我是一名初学者编程学生,所以感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

我猜您应该将While更改为If

private void btnAddItem_Click(object sender, EventArgs e)
{
    //Declare variables
    double dblSalesTax = 0, dblPrice, dblTax, dblSalesPrice;
    string strItem, strTaxAdded;
    int intQuantity;
    bool diffTest = false;

    //Process user input


        //While (!diffTest)
        //{

          diffTest = double.TryParse(txtSalesTax.Text, out dblSalesTax);

        //}   
        // Check if the value gets parsed and is in range, otherwise show error and 
        //exit from this handler
        If (!diffTest || dblSalesTax < 0 || dblSalesTax > 25)  '<--- Change While to If
        {

            MessageBox.Show("Please enter a valid tax.");
            txtSalesTax.Clear();

            diffTest = false;
            return; // Return from here since validation failed
        }

        ... 
        ...
        ...
}

答案 1 :(得分:0)

你不需要在那里发表声明。将其更改为:

if (dblSalesTax < 0 || dblSalesTax > 25)

因为你进入那个循环就有一个while循环,所以你永远不会离开,因为你的情况总是如此。

答案 2 :(得分:0)

您可以尝试将#include <stdlib.h> struct student_record { int student_id; int student_age; char first_name[21]; char last_name[22]; }; struct student_record_node { struct student_record *record; struct student_record_node *next; }; void parseFile(char *filename,struct student_record_node**head); int main(int argc, char* argv[]) { struct student_record_node *head; parseFile(argv[1], &head); return 0; } void parseFile(char *filename, struct student_record_node**head) { FILE *fp; fp = fopen(filename, "r"); //creates a POINTER to "filename" struct student_record_node *new_student; new_student = malloc(sizeof(struct student_record_node)); if(new_student == NULL) {printf("failed"); } fscanf(fp, "%s", new_student->record->first_name); /*problem here*/`` } Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());一起用于有您循环的await,如下所示:

task