자바 초보자는 여기에 있습니다. 루프가 여러 개 있습니다. 모든 분리 된 생각은 while 조건이 참일 때까지 순서대로 내려갈 것이라고 생각합니다. 내 결과물에서 제시하는 것은 처음 발견되는 루프가 실제로 발견되어 다른 것들을 보지 않고 종료된다는 것입니다. 이 작업을 수행하는 더 좋은 방법이 있는지 또는 명백한 오류가있는 경우 조언하십시오. (xCar = 3, yCar = 3) 및 Destination = (1,1)의 샘플 출력은 단지 "West" "West"입니다. 2 개의 "South"가 있어야합니다. * 인쇄 진술을 변명해라, 내가하고 있던 일을 디버깅하려했다. 또한 나는 '차'한 곳을 움직여 방향을 다시보고해야한다고 지적해야한다. 첫 번째 반면에while 루프에서 여러 조건이있는 루프
:
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);
}
방법 nextMove()는 클래스 방향의 열거를 호출은
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;
는 출력 무슨 일이 일어나고 무엇
Car [id = car17, location = [x=3, y=3], destination = [x=1, y=1]]
2nd if statement
WEST
2nd if statement
WEST
출력을 표시하십시오. – zEro
출력이 추가되었습니다 –
무슨 일이 일어나고 있는지 더 잘 이해하기 위해 디버깅을 추가하여 각 이동 전후의 위치를 출력 할 수 있습니다. –