简体   繁体   中英

Calling on Enum class for reference

I have this enum class listed below. Enum class is:

public enum Direction {

    NORTH,
    SOUTH,
    EAST, 
    WEST,
    NOWHERE
}

and instructions are when I move a vehicle from my other class called vehicle, it returns a direction (NORTH, SOUTH, EAST, or WEST) to move that will take the Car one step closer to the target destination. The call to nextMove() should modify the internal state of the Vehicle object to reflect its new location given the direction it returned. If the current location of the Car equals the target destination, then nextMove should return "NOWHERE". SO I tried to address this creating if statements, as in, if it goes north, then it will print NORTH and vice verse. Here are the specs and my attempt:

if a Car is at position (3, 10) and it decides to move north, then it will move to position (3, 11). In general, if a car moves "north" from position (x, y) it will end up in position (x, y+1), "south" from (x, y) puts it in position (x, y-1), "east" (x+1, y) and "west" (x-1, y). As in, if the object moves upwards, then it will say "NORTH."

Current Attempt:

        public void Direction(Direction togo)  
        {
            if((yDestination+1)>yDestination)
            {
                System.out.println("NORTH");
            }

            if((yDestination-1)<yDestination)
            {
                System.out.println("SOUTH");
            }
            if((xCar+1)>xCar)
            {
                System.out.println("EAST");
            }
            if((xCar-1)<xCar)
            {
                System.out.println("WEST");
            }

I have attempted to answer this object above by the if statements but im pretty sure something is wrong.

According to your code,all the conditions will execute. Try out this..

Class Vehicle
{
    private int currentX,CurrentY;

    public void printDirection(int modifiedX,int modifiedY)
     {
            if(modifiedY>CurrentY)
            {
                System.out.println(Direction.NORTH);
            }

            if(modifiedY<CurrentY)
            {
                System.out.println(Direction.SOUTH);
            }
            if( modifiedX>currentX)
            {
                System.out.println(Direction.EAST);
            }
            if(modifiedX<currentX)
            {
                System.out.println(Direction.WEST);
            }
            currentX=modifiedX;
            currentY=modifiedY;
         }

          public static void main()
           { 
             Vehicle myVehicle = new Vehicle();
             myVehicle.printDirection(0,1);
           }
}

Try this out.. xPosition and yPosition is the current (x,y) of the Car.

class Car
{
    int xPosition = 0;
    int yPosition = 0;

    public void nextMove(Direction direction)  
    {
        switch (direction)
        {
            case NORTH:
                yPosition += 1;
                break;
            case SOUTH:
                yPosition -= 1;
                break;
            case EAST:
                xPosition += 1;
                break;
            case WEST:
                xPosition -= 1;
                break;
        }
        System.out.println(direction);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM