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

2014/12/6

unreachable statement int

sjoerdieman sjoerdieman

2014/12/6

#
This code is giving me a unreachable statement. The following part of the code below is red marked: pause = 100; I dont get it, how can i fix this?
private int pause = 100;    

private boolean placeTimer()
    {
        if(pause>0)  
        {
            pause--;  
        }
        if(pause == 0)  
        {  
            return true;
            pause = 100;
        }  
        else return false;
    }

    public void placeGround()
    { 
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)  
        {  
            if (mouse.getButton() == 1 && boatClicked()==true && placeTimer() == true)  
            {  
                MouseInfo mousez = Greenfoot.getMouseInfo();
                getWorld().addObject(new PathingRed(), mousez.getX(), mousez.getY());
            }
        }
    }
davmac davmac

2014/12/6

#
Execution of a method stops at a 'return' statement. The return statement causes execution to return to the calling method. So, you can't do anything immediately after a return statement. In this case you could probably just swap lines 11 and 12 and the problem would be solved.
danpost danpost

2014/12/6

#
Any statement after a return within the same block will be an unreachable statement and will throw that error. Line 12 is the unreachable statement. It is in the same block of code and after line 11 -- a return statement. A return statement causes the method to terminate immediately, so it is pointless to place any statements after it within the same block.
sjoerdieman sjoerdieman

2014/12/7

#
Thanks it worked!
You need to login to post a reply.