Question

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
Était-ce utile?

La solution

What is happening is this:

In your first while loop you call your nextMove() method. This method is incrementing both x and y in your first loop, hence why you don't get the output of the other while loops. If you changed your input destination to [3,4] you should get output of WEST,WEST,SOUTH

You could fix this so that only one of the dimensions is incremented at a time in your nextMove() method by changing them to else if like this

public Direction nextMove() {
    if (xCar < xDestination){
        xCar = xCar + car.x+ 1;
    }
    else if (xCar > xDestination){
        xCar = xCar + car.x -1;
    }
    else if (yCar < yDestination){
        yCar = yCar + car.y +1;
    }
    else if (yCar > yDestination){
        yCar = yCar + car.y -1;
    }
    return null;

Autres conseils

I would not make a new class here. You only need a method to do the moving which can be created in the same file as the main function. If you really want a class to move the car then you need to declare it correctly. Remember, a Class requires a public, private and a constructor as well as all of the methods the class can do. It would also be easy to put the moving methods inside the car class since part of the car class should hold the location of the car object. I don't know if you want it to move across a screen or just change the location. If you want to move across the screen the while loops will work. But if all you need is for the location to change, then it would be much easier to change a private variable holding the location of the car; it will be easier to code and to run since evaluating booleans takes a sizable amount of computation time. Good luck. Let me know if you didn't understand anything.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top