无法在switch语句中使用enum

时间:2013-05-07 20:09:46

标签: java enums switch-statement

我试图在交换机案例中使用枚举。出于某种原因,交换机案例在netbeans中声明它期望一个int但是找到一个枚举。我如何在setStats()中使用enum作为我的switch case?我使用的是Java 7。

package rpgmanager;

import java.util.Random;

/**
 *
 * @author aaron
 */
public class CharacterClass {
    Random random = new Random();
    private int STR;
    private int DEX;
    private int CON;
    private int WIS;
    private int INT;
    private int CHA;
    private Enum SuperClass;

    /**
     *
     */
    public CharacterClass(Enum Player_Class) {
        SuperClass = Player_Class;
        System.out.println("You are a handsome "+ Player_Class);
        this.setStats();
    }

    /*
     * Sets the users stats
     */
    private void setStats() {
        STR = random.nextInt(6) + 5;
        DEX = random.nextInt(6) + 5;
        CON = random.nextInt(6) + 5;
        WIS = random.nextInt(6) + 5;
        INT = random.nextInt(6) + 5;
        CHA = random.nextInt(6) + 5;

        switch (SuperClass) {
            case Barbarian:
                break;
            default:
                throw new AssertionError(SuperClass.name());
        }
    }

根据请求我的ENUM代码 -

    private enum Base_Class {
            Barbarian, Bard, Cleric, Druid, Fighter, Monk, 
            Paladin, Ranger, Rogue, Sorcerer, Wizard
        };

  /*
         * Sets the Base Class of the PC
         */
        public void setBaseClass() {
            do {
                System.out.println("Available Classes: ");
                System.out.println("Please select a cooresponding number for class.");
                int i = 1;
                for(Base_Class value: Base_Class.values()){
                    System.out.println(i+": "+value.name());
                    i++;
                }
                try {
                    System.out.print("Choice: ");
                    Base_Class_Choice = user_input.nextInt();
                    Player_Class = Base_Class.values()[Base_Class_Choice - 1];
                    try {
                        CharacterClass extendedClass = new CharacterClass(Player_Class);
                        extendedClass.getStatsList();
                        Extended_Class = extendedClass;
                    } catch (Exception e) {
                        System.out.println("Failed to build the character class!");
                    }
                    if (Base_Class_Choice < 1 || Base_Class_Choice > i) {
                        System.out.println("Not a valid number.");
                    }
                } catch (Exception e) {
                    System.out.println("You must choose a valid class. Try numbers.");
                    user_input.next();
                }
            } while (Base_Class_Choice == 0);
        }

1 个答案:

答案 0 :(得分:5)

您必须使用特定的枚举类型,而不仅仅是Enum。例如,您可能有:

public enum PlayerClass {
    WARRIOR,
    BARBARIAN,
    WIZARD;
}

public class GameCharacter {

    private PlayerClass playerClass;

    public GameCharacter(PlayerClass playerClass) {
        this.playerClass = playerClass;
    }

    private void setStats() {
        ...
        switch (playerClass) {
            case BARBARIAN:
                ...
        }
    }
}

顺便说一句,我强烈建议您遵循Java命名约定 - 因此请将变量strengthdexterity等命名为STR或{ {1}}。