forloop 停止后如何运行代码?

How do i run a code after forloop has stopped?

您好,是否可以在 3 次尝试显示 pin 失败后显示消息框。目前我想要我的代码做的是,在 3 次尝试失败后,它将更新数据库中的一个新的随机 pin。所以当它更新后,我想显示一个消息框,显示一个新的 pin 已经发送。所以这是我当前的代码。

    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{}' 部分。但是尝试第二次失败的尝试时,它将显示消息框 "Invalid Pin. New Pin Has Been Sent."。这让我意识到,在第二次尝试时,'if' 将会运行。所以从技术上讲,密码在第二次尝试时就已经改变了。是否可以在第三次尝试之前不让它在第二次尝试中更改,并且在更新 pin 时显示消息框?

我也试过添加

if (i == 2) continue;

进入

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

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

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;

            }

在引脚分配时设置一个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;

}

您可以在 elseif 表达式中添加附加条件来检查循环索引。它小于 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;

        }

假设您为输入的每个错误 Pin 码调用 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");
}