caleb Kisling
caleb Kisling

Reputation: 11

Can be certain items in an array be compared for something to be true - C#

i have been having trouble getting this code to work is there something wrong with it or am i just doing it wrong

string a = teams[1];
string b = wins[1];
int numWins = 0;
while (o < wins.Length)
{                    
    if (a != b)
    {
        numWins++;
    }
    o++;                       
}

numOfWinsLabel.Text = numWins.ToString();

it is adding to the counter when both of them are equal in the txt files that i have set

Can someone plz help me?

Upvotes: 0

Views: 53

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

You seem to want to walk through the arrays, and you seem to try to use the o variable to point to each element. But you're not "telling" the arrays to actually use the o variable.

What happens in that case is that the variables get assigned the values of index 1 and they never change. It's also worth noting that arrays usually start at index 0.

Try the following:

int numWins = 0;
for (int o = 0; o < wins.Length; o++)
{                    
    string a = teams[o];
    string b = wins[o];

    if (a != b)
    {
        numWins++;
    }                     
}

numOfWinsLabel.Text = numWins.ToString();

This of course also assumes that the teams has at least as much elements as the wins array, or you'll get an exception.

I've changed the while to a for as it's more suitable for situations where you already know the number of times you want to loop.

Upvotes: 3

Related Questions