This site requires JavaScript, please enable it in your browser!
Greenfoot back
KeithM
KeithM wrote ...

2016/3/11

How to fix unreachable statement error, and have boolean return true??

KeithM KeithM

2016/3/11

#
When I place the "return true" statement in this position,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public boolean eat()
  {
      Food f1 = new Food();
      Actor f;
      f = getOneObjectAtOffset(0, 0, Food.class);
      if (f != null)
      {
          return true;
          World world;
          world = getWorld();
          world.removeObject(f);
          getWorld().addObject(f1, (int)(Math.random()*36+1), (int)(Math.random()*25+1));
          foodEaten += 1;
        
      }
      else
          return false;
      
  }
It gives me an unreachable statement error, however when i place it in this postion,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public boolean eat()
 {
     Food f1 = new Food();
     Actor f;
     f = getOneObjectAtOffset(0, 0, Food.class);
     if (f != null)
     {
         World world;
         world = getWorld();
         world.removeObject(f);
         getWorld().addObject(f1, (int)(Math.random()*36+1), (int)(Math.random()*25+1));
         foodEaten += 1;
         return true;
     }
     else
         return false;
     
 }
It never returns true. Any help is appreciated.
Super_Hippo Super_Hippo

2016/3/11

#
A 'return' exits the method. So nothing after that can executed. If you place it in line 8, then lines 9 through 13 are useless. How do you use the 'eat' method?
KeithM KeithM

2016/3/11

#
heres the whole class. I use it to remove the "food" class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class Snake extends Actor
    {
        private int foodEaten = 0;
        private FollowSnake fs = new FollowSnake();
        private int snakeLength = 1;
     
    public Snake()
    {
         
    }
   
    public void act()
    {
        move();
        eat();
        grow();
    }
     
    public void move()
    {
        move(1);
        if (Greenfoot.isKeyDown("left"))
        {
            turnTowards(getX()-1, getY());
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turnTowards(getX()+1, getY());
        }
        if (Greenfoot.isKeyDown("down"))
        {
            turnTowards(getX(), getY()+1); 
        }
        if (Greenfoot.isKeyDown("up"))
        {
            turnTowards(getX(), getY()-1); 
        }  
          
        if (getX() <= 0 || getX() > 38 || getY() <= 0 || getY() > 27)
        {
            Greenfoot.stop();
            return;
        }
    }  
     
    public boolean inBounds()
    {
        if (getX() <= 0 || getX() > 38 || getY() <= 0 || getY() > 27)
        {
            Greenfoot.stop();
            return false;
        }
        else
            return true;
    }    
        
     
    public boolean eat()
    {
        Food f1 = new Food();
        Actor f;
        f = getOneObjectAtOffset(0, 0, Food.class);
        if (f != null)
        {
            World world;
            world = getWorld();
            world.removeObject(f);
            getWorld().addObject(f1, (int)(Math.random()*36+1), (int)(Math.random()*25+1));
            foodEaten += 1;
            return true;
        }
        else
            return false;
        
    }
         
    private void grow()
    {
        for (int i = 0; i < foodEaten*3; i++)
        {         
        
        }
    }
     
    public int getsnakeLength()
    {
        return snakeLength;
    }
     
}
The grow method currently does nothing.
KeithM KeithM

2016/3/11

#
i understand this, however the boolean never = true. When i remove line 67, it is functional, however, then the food is never removed from the game board. (The food class contains no methods)
danpost danpost

2016/3/11

#
Replace lines 70 through 73 with the following:
1
2
}
return f != null;
This should not really change anything from the code it replaces; but, if ever a Food object is removed, which you seem to indicate happens with "When <such-n-such>, then the food is never removed...", the method must return a true value. All this is rather immaterial, as the value returned is not used through the call to the 'eat' method. That is, line 15:
1
eat();
calls the method, but ignores totally any returned value.
You need to login to post a reply.