mardi 8 décembre 2015

Error when returning from a method using loops and if statements

I have to write a method that accepts an integer to look for and returns the position of the first occurrence found or -1 if not found in an array. This is what I have so far, however, it has an error when returning but I do not know what I am doing wrong:

public static int findValue (int [] z, int y) //y is the number given by the user that I need to find.
{
    for (int x = 0; x < z.length ; x++) 
    {
        if ( z[x] == y)
        {
            int w = x;
            break;
            return (w);
        }
    }
    else 
        return -1;

}

Aucun commentaire:

Enregistrer un commentaire