0
public static int [] tallyResults (String number, String guess)
{
    int bulls = 0;
    int cows = 0;
    for (int i=0;i<number.length();i++)
    {
        if (number.charAt(i)==guess.charAt(i))
        {
            bulls = bulls + 1;
        }
        else if (contains(number, guess.charAt(i)))
        {
            cows = cows + 1;
        }
        else
        {
            continue;
        }
    cows = cows - bulls;
    int [] count = {bulls, cows};
 }
 return count;

}

This is a function I'm creating that's part of a larger problem for my CS class. I need to return the array that I declared. However, when I attempt to compile, I get an error:

BullsAndCows.java:94: error: cannot find symbol
         return count;
                ^
  symbol:   variable count
  location: class BullsAndCows

I've messed around with it for a bit and looked online for answers, but nothing seems to do the trick.

  • 2
    The scope of the variable count is only within the "for" block. You are getting an error because you are using that variable outside the block. – Superchamp Feb 05 '22 at 01:55

1 Answers1

2

The count array is defined inside your for loop and thus it cannot be used outside of it.

Try to modify your code as

public static int [] tallyResults (String number, String guess)
{
    int bulls = 0;
    int cows = 0;
    for (int i=0;i<number.length();i++)
    {
        if (number.charAt(i)==guess.charAt(i))
        {
            bulls = bulls + 1;
        }
        else if (contains(number, guess.charAt(i)))
        {
            cows = cows + 1;
        }
        else
        {
            continue;
        }
        cows = cows - bulls;
    }
    return new int[]{bulls, cows}; //count
}

If I correctly understand the logic of your code.

Eddie Deng
  • 1,399
  • 1
  • 18
  • 30