Solved Annoying Boolean Problem

Discussion in 'Plugin Development' started by _Belknap_, Apr 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    _Belknap_

    Hi, I have this problem. You see, I have 1 method that run through a list of dungeons and the players in the dungeons. It checks if the player is in that dungeon and also checks the dungeon's status. The annoying part is that it always returns false, because all the dungeons except 1 return false because they don't have the player, while that only 1 dungeon does. So how could I get it so that if the method returns true, it stays true and doesn't return false for all the other dungeons that returned false? Here is my boolean/method:
    Code:java
    1. public boolean isPlayerInGame(Player p) {
    2. Iterator<String> ds = dungeons.iterator();
    3. while (ds.hasNext()) {
    4. String dName = ds.next();
    5. DungeonManager d = dungeons1.get(dName);
    6. if (d.isInGame() && d.hasPlayer(p)) {
    7. return true;
    8. }else{
    9. return false;
    10. }
    11. }
    12. return false;
    13. }
     
  2. Offline

    Rocoty

    Remove the else statement. Just because the first one evaluates to false does not mean you don't want to evaluate the rest
     
  3. Offline

    _Belknap_

    Rocoty
    Wow, I totally forgot that :D
     
Thread Status:
Not open for further replies.

Share This Page