0

I´m trying to read the array below. What i want to do is just verify if the number 1 and 2 exists at the same time in the array. The problem is with my method. I keep getting an "use of unassigned local variable 'jogavel1'", that is inside my method. Can someone help or explain what i am doing wrong? Thanks a lot for your participation =).

        int[,] tabuleiro = new int[8, 8] {
            {1, 0, 1, 0, 1, 0, 1, 0},
            {0, 1, 0, 1, 0, 1, 0, 1},
            {1, 0, 1, 0, 1, 0, 1, 0},
            {0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0},
            {0, 2, 0, 2, 0, 2, 0, 2},
            {2, 0, 2, 0, 2, 0, 2, 0},
            {0, 2, 0, 2, 0, 2, 0, 2},
        };

        int numero = verificar(tabuleiro);

        Console.ReadKey();
    }


    public static int verificar(int[,] tabuleiro) {
        int jogavel, jogavel1, jogavel2 = 0;
        for (int i = 0; i < 7; i++) {
            for (int a = 0; a < 7; a++) {
                if (tabuleiro[i, a] == 1) {
                    jogavel1++;
                }
                else if (tabuleiro[i, a] == 2) {
                    jogavel2++;
                }
            }
        }
        if (jogavel1 > 0 && jogavel2 > 0) {
            jogavel = 1;
        }
        else
            jogavel = 0;
        return jogavel;
    }
}

2 Answers 2

2

Your variable instantiation is the problem:

int jogavel, jogavel1, jogavel2 = 0;

Doing it this leads to only having jogavel2 being assigned a value. Instead you should assign them like this:

int jogavel; // in your case this variable doesn't necessarily needs to be assigned
int jogavel1 = 0;
int jogavel2 = 0;

Explanation: Within your for-loop you use jogavel1 and jogavel2 and increment them using the ++-operator. This isn't possible for unassigned variables, which applies to jogavel1. On the other hand jogavel doesn't necessarily needs to be assigned a value to as you don't use it for a direct calculation. Instead you just assign a value to it in your if-else-statement, which is fine for the compiler.

By the way there's an error in your logic: Within your loops you don't fetch the last elements from the array. You use i < 7 resp. a < 7 as a break condition, which is not correct as your jagged array has 8 elements in each dimension. So you better edit it like this:

...
for (int i = 0; i < tabuleiro.GetLength(0); i++) // array-length of 1. dimension (=8)
{
    for (int a = 0; a < tabuleiro.GetLength(1); a++) // len. of 2. dimension (=8)
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! That's what i was expecting! =)
0

Local variables aren't initialized. You have to manually initialize them.

So you code should be

int jogavel, jogavel1=0, jogavel2 = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.