如何使用带有switch语句的枚举?

时间:2016-03-01 23:07:39

标签: java enums switch-statement

所以我在网站上看了几个例子,但没有找到任何需要的东西。我尝试做的练习是将枚举类链接到页面底部的switch语句。

这个小部分的练习题是:"谈话方法应该要求用户输入一个短语,该短语应该打印在控制台上。 娱乐机器人可以在四个方向(向前,向后,向左,向右)中的任何一个方向上行走任意数量的步伐。因此,步行方法应该询问用户他们希望机器人采取多少步伐以及在哪个方向上。请注意,您必须验证用户的输入。方向必须存储为枚举并适当访问。"

枚举类:

package robot;

public enum Directions {
    FORWARD, BACKWARD, LEFT, RIGHT;
}

主要课程:

package robot;

import java.util.Scanner;

public class EntertainmentRobot extends Robot implements Walk, Talkable {

    private Directions direction;

    public EntertainmentRobot(double height, double weight, String manufacturer, String name) {
        super(height, weight, manufacturer, name);
        setEnergy(10);
        setEnergyUnitsRequired(0.75);
    }

    // ACCESSOR METHOD
    public Directions getDirection() {
        return direction;
    }

    // MUTATOR METHOD
    public void setDirection(Directions direction) {
        this.direction = direction;
    }

    @Override
    public void start() {
        System.out.println("-----I AM THE ENTERAINIMENT ROBOT-----");
    }

    @Override
    public void stop() {
        System.out.println("I have stopped entertaining people.");
    }

    @Override
    public void doTask() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doTask(Scanner in) {
        System.out.println("I am entertaining people.");
        System.out.println("How many people would you like to play with me?");
        int times = in.nextInt();
        for (int i = 0; i < times; i++) {
            play();
        }
        System.out.println(" ");
    }

    public String toString() {
        return "[Name= " + getName() + "\nWeight= " + getWeight() + "\nHeight:" + getHeight() + "\nManufacturer: "
            + getManufacturer() + "\nPurpose: " + getPurpose() + "]";
    }

    public void play() {
        if (getEnergy() >= energyRequired()) {
            energyComsumption();
        } else {
            System.out.println("\n-----WHOOPS-----");
            System.out.println("I do not have enough energy to study");
            regenerate();
            System.out.println("I must get more energy");
            System.out.println("I have regenerated my energy");
        }
    }

    @Override
    public void talk(Scanner in) {
        System.out.println("Please enter a phrase for me to speak: ");
        String talk = in.nextLine();
        System.out.println("You asked me to say the following phrase: " + talk);
    }

    @Override
    public void walk(Scanner in) {
        System.out.println("Would you like me to walk? [YES/NO]");
        String walk = in.nextLine();
        if (walk.equalsIgnoreCase("Yes")) {
        }
    }

    public void directionChoice(Scanner in) {
        System.out.println("For how many paces?");
        int paces = in.nextInt();
        System.out.println("1 - FORWARD" + "2 - BACKWARD" + "3 - LEFT" + "4 - RIGHT");
        switch (paces) {
        case 1: 
            break;
        case 2:
        }
    }
}

2 个答案:

答案 0 :(得分:1)

以下是Direction案例中如何使用switch枚举的示例:

Direction direction = //get direction input
switch(direction){
case BACKWARD:
    //do something
    break;

case FORWARD:
    //do something
    break;

case LEFT:
    //do something
    break;

case RIGHT:
    //do something
    break;
}

但是,在执行此开关之前,我们需要获取此枚举的值,我想这将是用户输入(String)。我们可以使用枚举的valueOf()方法来获取实际值。 请注意,如果输入不是其中一个值,则valueOf()会抛出IllegalArgumentException,因此我们可以执行以下操作来验证用户输入:

String input = //get user's input
Direction direction;
try{
    direction = Direction.valueOf(input);
}catch(IllegalArgumentException iae){
    //Invalid input
}

答案 1 :(得分:1)

尝试以下方法:

public enum Direction {
    FORWARD, BACKWARD, LEFT, RIGHT;

    public static Direction getDirection(String direction) {
        for (Direction dir : values()) {
            if (dir.name().equalsIgnoreCase(direction)) {
                return dir;
            }
        }
        return null;
    }
}

...

public void directionChoice(Scanner in) {
    System.out.println("For how many paces?");
    int paces = Integer.parseInt(in.nextLine()); // To avoid doing nextInt and then nextLine
    System.out.println("In which direction: FORWARD, BACKWARD, LEFT or RIGHT?");
    Direction direction = Direction.getDirection(in.nextLine());

    while(direction == null) {
        System.out.println("Invalid direction!");
        System.out.println("In which direction: FORWARD, BACKWARD, LEFT or RIGHT?");
        direction = Direction.getDirection(in.nextLine());
    }

    switch (direction) {
    case FORWARD:
        // do stuff
        break;
    case BACKWARD:
         // do stuff
        break;
    case LEFT:
        // do stuff
        break;
    case RIGHT:
        // do stuff
        break;
    default:
        // do stuff (or not)
        break;
    }
}