Home > Mobile >  Cant hide buttons given a condition "Health Bar"
Cant hide buttons given a condition "Health Bar"

Time:09-21

I'm comparing 2 lists, when I click the submit button it compares the lists. if the lists didn't match the health bar, the block should hide 1 bar every time it can't compare.

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

        }

CodePudding user response:

This:

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

Can be simplified to this:

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

It's "hiding all three" because you're explicitly telling it to do that. Consider the logic you've written:

  • Set a to 0.
  • While (instead of if for some reason?) a is 0 (which it is, because you just set it to that)
    • Hide the first thing
    • Add 1 to a
  • While (instead of if for some reason?) a is 1 (which it is, because you just added 1 to 0, which is 1)
    • Hide the second thing
    • Add 1 to a
  • While (instead of if for some reason?) a is 2 (which it is, because you just added 1 to 1, which is 2)
    • Hide the third thing
    • Add 1 to a

It sounds like you want to track the number of times the user has clicked across multiple calls to this logic. For that you'd need to store a value outside this method. Consider a class-level value:

class WhateverYourClassIs
{
    private int numberOfClicks = 0;

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

Then in your method you would examine that value. You would use if statements for this, not loops (since you don't want to actually repeat anything, you're just checking a logical condition). And you wouldn't want to re-check the value to see if it's been increased right after you've increased it, because that will always be true.

So perhaps something like this:

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

So with each click this.numberOfClicks is always incremented. But before incrementing it you check what the current value is and perform your logic.

  • Related