如何在 forloop 停止后运行代码

本文关键字:运行 代码 forloop | 更新日期: 2023-09-27 18:36:36

嗨,是否可以在 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{}"部分中。但是尝试第二次失败的尝试,它将显示消息框"无效的 Pin。新图钉已发送。这让我意识到,在第二次尝试时,"如果"会运行。因此,从技术上讲,引脚在第二次尝试时已经更改了。是否可以在第二次尝试之前不让它更改,直到第三次尝试,并在引脚更新时显示消息框?

我也尝试添加

if (i == 2) continue;

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

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

附言,对不起,如果这个问题以前已经回答过。并提前感谢您的帮助。

我有点只是这样做的

            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;
            }

如何在 forloop 停止后运行代码

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

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

您可以在 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;
        }