0

I have an array of Pokemons. Pokemons have an abstract method vitesse(vitesse = speed in french), which change depending of the type of pokemon.

I also have a class TabPokemon which generate my array of pokemons. In this class I want to calculate the fastest pokemon.

This is my method :

public Pokemon plusRapide()
{
    Pokemon winner;
    double vitesse = 0.0;
    foreach(Pokemon p in tab)
    {
        if(p.vitesse()> vitesse)
        {
            vitesse = p.vitesse();
            winner = p;
        }

    }
    return winner;
}

It shows me an error on the return statement because it is assigned locally. How can I return the object Pokemon with the greatest speed value?

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
rn605435
  • 175
  • 11

1 Answers1

3

Initialize the Pokemon object by Pokemon winner = null; on the first line of the method.

kaffekopp
  • 1,979
  • 4
  • 10