Java newbie here. I have multiple while loops. All separated thinking that it would just go down in order until the while condition equals true. What my output suggests is that it does the first while loop it finds that is true then exits, with out looking at the others. Please advise if there is a better way doing this or if you see an obvious error. Sample output from (xCar =3, yCar =3) and Destination = (1,1) is just "West" "West". There should be 2 "South". *Please excuse the print statements, I was trying to debug what it was doing. I should also point out that I can only move the 'car' one spot then need to report back the direction.
if (car.getLocation().equals(car.getDestination())){
System.out.println("inside this if statement");
System.out.println(car.nextMove().NOWHERE);
}
//Seeing if Xcar is greater than Xdest. If so moving west
while (car.getxCar() > xDestination){
System.out.println("2nd if statement");
System.out.println(car.nextMove().WEST);
}
//Seeing if Xcar is less than Xdest. If so moving east
while (car.getxCar() < xDestination){
//System.out.println("3rd");
System.out.println(car.nextMove().EAST);
}
//Seeing if Ycar is greater than Ydest. If so moving south
while (car.getyCar() > yDestination){
System.out.println("4th");
System.out.println(car.nextMove().SOUTH);
}
//Seeing if Ycar is less than Ydest. If so moving north
while (car.getyCar() < yDestination){
System.out.println("5th");
System.out.println(car.nextMove().NORTH);
}
METHOD nextMove() it is calling a enum in class Direction
public Direction nextMove() {
if (xCar < xDestination){
xCar = xCar + car.x+ 1;
}
if (xCar > xDestination){
xCar = xCar + car.x -1;
}
if (yCar < yDestination){
yCar = yCar + car.y +1;
}
if (yCar > yDestination){
yCar = yCar + car.y -1;
}
return null;
Output
Car [id = car17, location = [x=3, y=3], destination = [x=1, y=1]]
2nd if statement
WEST
2nd if statement
WEST
nullfrom yournextMove()method?