如何在for循环停止后运行代码?

时间:2015-01-07 05:31:39

标签: c# arrays loops for-loop

嗨,可以在3次尝试引脚显示失败后显示消息框。目前我想让我的代码做的是,在3次尝试失败后,它将更新数据库中的新随机引脚。因此,当它更新后,我想显示一个消息框,显示已发送新的引脚。所以这是我目前的代码。

    string[] pinArray = new string[3];
    private void PinError()
    {

        for (int i = 0; i < pinArray.Length; i++)
        {


            if (pinArray[i] != null)
            {

                string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("Update PostParcel set Pin=@pin1 where Box='1'and Pin!= '' ", myConnect);
                cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
                myConnect.Open();
                cmd.ExecuteNonQuery();
                myConnect.Close();
            }
            else if (pinArray[i] == null)
            {
                pinArray[i] = txtpBox1.Text;

                MessageBox.Show("Invalid Pin. Pin Will Change After " + (3 - i) + " More Attempts");

                break;

            }

        }
    }

我尝试过放置

MessageBox.Show("Invalid Pin. New Pin Has Been Sent.");
break;

在最后的“if {}”部分。但是尝试第二次失败的尝试,它将显示消息框“无效的针。新针已被发送。”。这让我意识到,在第二次尝试中,'if'将会运行。从技术上讲,引脚在第二次尝试时已经改变了。是否有可能在第二次尝试之前不让它改变,直到第三次尝试,并且在引脚更新时显示消息框?

我也尝试过添加

if (i == 2) continue;

if (pinArray[i] != null){  **if (i == 2) continue;** }

但在第二次尝试时,它只会显示已发送新引脚

P.s,对不起,如果之前已经回答了这个问题。并提前感谢您的帮助。

我有点这样做了

            if (pinArray[i] != null)
            {
                if (i == 2) continue;
                string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("Update PostParcel set Pin=@pin1 where Box='1'and Pin!= '' ", myConnect);
                cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
                myConnect.Open();
                cmd.ExecuteNonQuery();
                myConnect.Close();
                MessageBox.Show("Invalid Pin. New Pin Has Been Sent");
                break;

            }

3 个答案:

答案 0 :(得分:2)

分配引脚时设置bool

for (int i = 0; i < pinArray.Length; i++)
{


    if (pinArray[i] != null)
    {

        string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);
        SqlCommand cmd = new SqlCommand("Update PostParcel set Pin=@pin1 where Box='1'and Pin!= '' ", myConnect);
        cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
        myConnect.Open();
        cmd.ExecuteNonQuery();
        myConnect.Close();

        assigned = true;
    }
}

if (!assigned)
{
    pinArray[i] = txtpBox1.Text;

    MessageBox.Show("Invalid Pin. A new one will be assigned");

    break;

}

答案 1 :(得分:1)

假设您为输入的每个错误引脚致电PinError(),请尝试以下代码

int i = 1;

private void PinError()
{
     if(i != 3)
     {
         MessageBox.Show("Invalid Pin. Pin Will Change After " + (3 - i) + " More Attempts");
         i++;
         return;
     }

     string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
     SqlConnection myConnect = new SqlConnection(strConnectionString);
     SqlCommand cmd = new SqlCommand("Update PostParcel set Pin=@pin1 where Box='1'and Pin!= '' ", myConnect);
     cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
     myConnect.Open();
     cmd.ExecuteNonQuery();
     myConnect.Close();
     MessageBox.Show("Invalid Pin. New Pin Has Been Sent");
}

答案 2 :(得分:0)

您可以在elseif表达式中添加其他condidition以检查循环索引。它不到2(因为你开始索引是0,你有3个循环)你不会显示弹出窗口。如下所示:

else if (pinArray[i] == null && **i > 2**)
        {
            pinArray[i] = txtpBox1.Text;

            MessageBox.Show("Invalid Pin. Pin Will Change After " + (3 - i) + " More Attempts");

            break;

        }