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,421,763
  • 867
  • 9,128
  • 9,194
rn605435
  • 175
  • 2
  • 12

1 Answers1

3

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

kaffekopp
  • 2,551
  • 6
  • 13