So my code is meant to compare the y co-ordinate of the robot with the y co-ordinate of the target. I'm having problems making the function return anything when I add the print statements in. I have a feeling this has something to do with brackets, but I'm not exactly sure how to use them.
This is not the entire program, but it's the only bit with errors in it.
When I try to compile this:
public class Ex12
{
private byte isTargetNorth(IRobot robot)
{
if (robot.getLocationY() > robot.getTargetLocation().y) {
return 1; System.out.println("north");
} else if (robot.getLocationY() == robot.getTargetLocation().y) {
return 0; System.out.println("no");
} else {
return -1; System.out.println("south");
}
}
}
I get error: unreachable statement
When I try this:
public class Ex12
{
private byte isTargetNorth(IRobot robot)
{
if (robot.getLocationY() > robot.getTargetLocation().y)
return 1; System.out.println("north");
else if (robot.getLocationY() == robot.getTargetLocation().y)
return 0; System.out.println("no");
else
return -1; System.out.println("south");
}
}
I get error: 'else' without 'if'
I get no errors when I remove the System.out.println() functions.
The return statements exit your method. So, the System.out's will never be called.