如何在 FOR 循环中编写代码 运行?而不是那么多IF

How do I make the code run in a FOR loop? Instead of so many IF

如何在此代码中使用 FOR 循环? 而不是使用很多 IF 个问题。 我已经尝试了很多方法来让它工作但没有成功。 我很乐意通过智能循环帮助我解决这个问题。 这是用于计算货架放置和抽屉放置的软件。 我需要你帮我解决这个问题。 我想要这样的东西:

            for (int i = 1; i <= NishCount; i++)
            {
                lblNish + i + .Text = nish + i + .ToString();
            }

这是代码:

            if (NishCount == 1 && MdfCount == 0)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = "ENTER VALUES";

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = "ENTER VALUES";
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 2 && MdfCount == 0)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = nish2.ToString();

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = "ENTER VALUES";
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 1 && MdfCount == 1)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = "ENTER VALUES";

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 2 && MdfCount == 1)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = nish2.ToString();

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 1 && MdfCount == 2)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = "ENTER VALUES";

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = MDF2.ToString();
            }
            if (NishCount == 2 && MdfCount == 2)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = nish2.ToString();

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = MDF2.ToString();
            }
            if (NishCount == 1 && MdfCount == 0 && SNCheckBox.Checked == true)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = "ENTER VALUES";

                lblSho.Text = shocev.ToString();

                lblMDF1.Text = "ENTER VALUES";
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 2 && MdfCount == 0 && SNCheckBox.Checked == true)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = nish2.ToString();

                lblSho.Text = shocev.ToString();

                lblMDF1.Text = "ENTER VALUES";
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 1 && MdfCount == 1 && SNCheckBox.Checked == true)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = "ENTER VALUES";

                lblSho.Text = shocev.ToString();

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 2 && MdfCount == 1 && SNCheckBox.Checked == true)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = nish2.ToString();

                lblSho.Text = shocev.ToString();

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = "ENTER VALUES";
            }
            if (NishCount == 1 && MdfCount == 2 && SNCheckBox.Checked == true)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = lblSho.ToString();

                lblSho.Text = "ENTER VALUES";

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = MDF2.ToString();
            }
            if (NishCount == 2 && MdfCount == 2 && SNCheckBox.Checked == true)
            {
                lblNish1.Text = nish1.ToString();
                lblNish2.Text = nish2.ToString();

                lblSho.Text = shocev.ToString();

                lblMDF1.Text = MDF1.ToString();
                lblMDF2.Text = MDF2.ToString();
            }

您可以先将它们全部初始化:

lblNish1.Text = "הזן מידות";
lblNish2.Text = "הזן מידות";

lblSho.Text = "הזן מידות";

lblMDF1.Text = "הזן מידות";
lblMDF2.Text = "הזן מידות";

然后根据需要进行设置:

if (NishCount >= 1) 
    lblNish1.Text = nish1.ToString();

if (NishCount >= 2) 
    lblNish2.Text = nish2.ToString();

if (SNCheckBox.Checked)
    lblSho.Text = shocev.ToString();

if (MdfCount >= 1) 
    lblMDF1.Text = MDF1.ToString();

if (MdfCount >= 2) 
    lblMDF2.Text = MDF2.ToString();

如果我们知道您的所有代码,可能会有更优雅的解决方案。

不要使用 for 循环。您的逻辑简化为以下

            lblNish1.Text = nish1.ToString();
            lblNish2.Text = "ENTER VALUES";

            lblSho.Text = "ENTER VALUES";

            lblMDF1.Text = "ENTER VALUES";
            lblMDF2.Text = "ENTER VALUES";

            if (NishCount == 2)
            {
                lblNish2.Text = nish2.ToString();
            }
            switch(MdfCount)
            {
                case 1:
                    lblMDF1.Text = MDF1.ToString();
                    break;
                case 2:
                    lblMDF1.Text = MDF1.ToString();
                    lblMDF2.Text = MDF2.ToString();
                    break;
            }
            if(SNCheckBox.Checked && !((NishCount == 1) && (MdfCount == 2)))
            {
                lblSho.Text = shocev.ToString();
            }