[Bluej-discuss] Exercise 10.14 page 276
Davin McCall
davmac at bluej.org
Thu Apr 6 03:01:04 BST 2006
Hi Luis,
You have two choices. One of them you have already mentioned: move the
declaration of the variable outside the while loop.
The other option is to put the return statement inside the while loop.
In this case you may find the compiler will complain if you don't also
have a return statement outside the loop, however.
Personally I'd recommend the first option. It's often nice to have only
a single return statement per method; it can make it easier to follow
the control flow of the method.
Davin
Luis Moreira wrote:
>
> Hi all,
>
> I am trying to do exercise 10.14 and I am having a problem with a
> variable declaration.
>
> This variable is declared inside aa while loop in the findFood method
> in Class Fox:
>
>
>
> Location where = (Location) adjacentLocations.next();
>
>
>
> When I try to return this variable with: return where, which is
> outside the while loop, I get an error because the compiler can not
> find this variable.
>
> I understand why is doing it, but apart from declaring this variable
> on the beginning of the method is there any other way to make it work.
>
> Best regards
>
> Luis
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060406/648e65f9/attachment.html
More information about the bluej-discuss
mailing list