无法在给定条件下隐藏按钮 "Health Bar"

Cant hide buttons given a condition "Health Bar"

我正在比较 2 个列表。当我单击提交按钮时,它会比较列表。如果列表与健康栏不匹配,该块应该在每次无法比较时隐藏 1 栏。

Click here to see a picture of the health bar

        if (Input.SequenceEqual(OrderedList))
        {
            richTextBox1.Text = "true both are 
        equal";
           
        }
        else
        {

            richTextBox1.Text = "false";

            int a = 0;
            while(a == 0)
            {   
                Health1.Hide();
                a++;
            }
            while (a == 1)
            {
                Health2.Hide();
               
                a++;
            }
            while (a == 2)
            {
                Health3.Hide();
                a++;
            }
      

        }

这个:

int a = 0;
while(a == 0)
{   
    Health1.Hide();
    a++;
}
while (a == 1)
{
    Health2.Hide();
    a++;
}
while (a == 2)
{
    Health3.Hide();
    a++;
}

可以简化为:

Health1.Hide();
Health2.Hide();
Health3.Hide();

它是“隐藏所有三个”,因为您明确告诉它这样做。考虑您编写的逻辑:

  • a 设置为 0
  • 虽然(出于某种原因而不是 if?)a0(确实如此,因为您只是将其设置为那个)
    • 隐藏第一件事
    • 1 添加到 a
  • 虽然(出于某种原因而不是 if?)a1(确实如此,因为您刚刚将 1 添加到 0 , 即 1)
    • 隐藏第二件事
    • 1 添加到 a
  • 虽然(出于某种原因而不是 if?)a2(确实如此,因为您刚刚将 1 添加到 1 , 即 2)
    • 隐藏第三件事
    • 1 添加到 a

听起来像您想跟踪用户在多次调用此逻辑时单击的次数。为此,您需要在此方法 之外存储一个值。考虑一个 class 级别的值:

class WhateverYourClassIs
{
    private int numberOfClicks = 0;

    // the rest of your class members, methods, etc.
}

然后在您的方法中您将检查该值。你会为此使用 if 语句,而不是 loops (因为你不想实际重复任何东西,你只是检查一个逻辑条件)。而且您不想 重新检查 该值以查看它是否在您增加后立即增加,因为这将 总是 是真的。

所以也许是这样的:

if (this.numberOfClicks == 0)
    Health1.Hide();
else if (this.numberOfClicks == 1)
    Health2.Hide();
else if (this.numberOfClicks == 2)
    Health3.Hide();
this.numberOfClicks++;

所以每次点击 this.numberOfClicks 总是递增。但在递增它之前,您检查当前值是什么并执行您的逻辑。