1

I am attempting to create a method by which I can return the value of an enum set by the users input. I decalred an enum, then I try to set the enum by the users input. I am unable to use my GetPlayerClass method to return the value of Player_Class. Can someone please tell me if this is the correct way to use an enum to set a value or is there a way I can allow the user to select from the enum list and let me get their choice.

My code is below.

TLDR: I am "enum dumb" and can't figure out how to set the value of a variable from an enum and then return that value later. Please view my code and let me know what I am doing wrong.

  // The PCs base class
    private enum Base_Class {
        Barbarian, Bard, Cleric, Druid, Fighter, Monk, 
        Paladin, Ranger, Rogue, Sorcerer, Wizard
    };


 // Setting the base class
 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 {
                Base_Class_Choice = user_input.nextInt();
                switch(Base_Class_Choice) {
                    case 1:
                        Base_Class Player_Class;
                        Player_Class = Base_Class.Barbarian;
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                System.out.println("You must choose a valid class. Try numbers.");
                user_input.next();
            }
        } while (Base_Class_Choice == 0);
    }

    /**
     * Return my players class
     * @return
     */
    public String getPlayerClass() {
        return Player_Class;
    }

I updated the code below My try area now looks like so.

    try {
                Base_Class_Choice = user_input.nextInt();
                Base_Class Player_Class = Base_Class.values()[Base_Class_Choice - 1];
            } catch (Exception e) {
                System.out.println("You must choose a valid class. Try numbers.");
                user_input.next();
            }

But the return for the Base_Class Player_Class does not work.

    public String getPlayerClass() {
        return Player_Class;
    }

When I try to return Player_Class it still fails out with the error cannot find symbol. What could I still be doing wrong?

UPDATE!!!! "ALL THE CODE!!!"

/*
 * 
 * 
 */
package rpgmanager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 *
 * @author Aaron
 */
public class Character {
        // The person playing the character
        private String Player_Name;
        // The PCs First Name
        private String First_Name;
        // The PCs Last Name
        private String Last_Name;
        // The PCs Race
        private enum Race {
            Dwarf, Halfling, Elf, Human, Gnome, HalfOrc, HalfElf
        };
        // The PCs base class
        private enum Base_Class {
            Barbarian, Bard, Cleric, Druid, Fighter, Monk, 
            Paladin, Ranger, Rogue, Sorcerer, Wizard
        };
        private Base_Class Player_Class;
        // Base Class Choice for switch case
        private int Base_Class_Choice = 0;
        // The PCs Height
        private int Height = 0;
        // The PCs Weight
        private int Weight = 0;
        // The PCs Age
        private int Age = 0;
        // The PCs base level
        private int Base_Level;
        // Sets up the scanner for inputs
        Scanner user_input = new Scanner(System.in);
        // Create a Buffered reader for input
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        // This instantiates a new character
        public Character() {

        }

        /**
         * Sets the PCs first name
         */
        public void setPlayerName() {
            System.out.print("Players Name: ");
            try {
                Player_Name = reader.readLine(); 
            } catch (IOException ioe) {
                System.out.println("The Player Name input failed to set.");
                System.out.println(ioe);
            }
        }

        /**
         * Sets the PCs first name
         */
        public void setFirstName() {
            System.out.print("First Name: ");
            try {
                First_Name = reader.readLine();
            } catch (IOException ioe) {
                System.out.println("The PCs First Name input failed to set.");
                System.out.println(ioe);
            }
        }

        /**
         * Sets the PCs last name
         */
        public void setLastName() {
            System.out.print("Last Name: ");
            try {
                Last_Name = reader.readLine();
            } catch (IOException ioe) {
                System.out.println("The PCs Last Name input failed to set.");
                System.out.println(ioe);
            }
        }

        /**
         * Sets the height of the PC
         */
        public void setHeight() {
            do {
                System.out.print("Height (inches): ");
                try {
                    Height = user_input.nextInt();
                    if(Height < 1) {
                        System.out.println("Not a valid number.");
                    }
                } catch(Exception e) {
                    System.out.println("You must use a number greater than 0!");
                    user_input.next();
                }
            } while (Height < 1);
        }

        /**
         * Sets the weight of the PC
         */
        public void setWeight() {
            do {
                System.out.print("Weight (pounds): ");
                try {
                    Weight = user_input.nextInt();
                    if (Weight < 1) {
                        System.out.println("Not a valid number.");
                    }
                } catch (Exception e) {
                    System.out.println("You must use a number greater than 0!");
                    user_input.next();
                }
            } while (Weight < 1);
        }

        /**
         * Sets the age of the PC
         */
        public void setAge() {
            do {
                System.out.print("Age (whole years): ");
                try {
                    Age = user_input.nextInt();
                    if (Age < 1) {
                        System.out.println("Not a valid number.");
                    }
                } catch (Exception e) {
                    System.out.println("You must use a number greater than 0!");
                    user_input.next();
                }
            } while (Age < 1);
        }

        /**
         * Sets the base level of the PC
         */
        public void setBaseLevel() {
            do {
                System.out.print("Starting Level: ");
                try {
                    Base_Level = user_input.nextInt();
                    if (Base_Level < 1 || Base_Level > 25) {
                        System.out.println("Not a valid number.");
                    }
                } catch (Exception e) {
                    System.out.println("You must choose a valid level between 1 and 25!");
                    user_input.next();
                }
            } while (Base_Level < 1 || Base_Level > 25);
        }

        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 {
                    Base_Class_Choice = user_input.nextInt();
                    Base_Class Player_Class = Base_Class.values()[Base_Class_Choice - 1];
                } catch (Exception e) {
                    System.out.println("You must choose a valid class. Try numbers.");
                    user_input.next();
                }
            } while (Base_Class_Choice == 0);
        }

        /**
        * Gets the PCs first name
        * @return 
        */
        public String getFirstName() {
            return First_Name;
        }

        /**
         * Gets the PCs last name
         * @return 
         */
        public String getLastName() {
            return Last_Name;
        }

        /**
         * Gets the PCs height
         * @return 
         */
        public int getHeight() {
            return Height;
        }

        /**
         * Gets the PCs age
         * @return 
         */
        public int getAge() {
            return Age;
        }

        /**
         * Gets the PCs base level (1-25)
         * @return
         */
        public int getBaseLevel() {
            return Base_Level;
        }

        /**
         * Gets the PCs player name
         * @return
         */
        public String getPlayerName() {
            return Player_Name;
        }

        /**
         *
         * @return
         */
        public Base_Class getPlayerClass() {
            return Player_Class;
        }

        /**
         *  Creates a new character
         */
        public void createCharacterNew() {
            this.setPlayerName();
            this.setFirstName();
            this.setLastName();
            this.setHeight();
            this.setWeight();
            this.setAge();
            this.setBaseLevel();
            this.setBaseClass();
        }
}
4

4 回答 4

2

It's going to continue to fail as long as you are declaring and accessing that variable in different scopes (i.e. two functions). If you want to assign it in one scope, and access it in another you are going to need to make it a member variable of a wrapping class.

Your question is misleading, because you don't want to assign an enum the users input, you want to select the proper enum based on user input.

I offer you a simplified case of your original problem in ideone where you can see what I'm talking about in action.

import java.util.Scanner;

class Player {
    private BaseClass playerClass;

    public void setPlayerClass() {
        Scanner in = new Scanner(System.in);
        StringBuilder output = new StringBuilder("Select a class:\n");
        BaseClass[] classes = BaseClass.values();
        for (int i = 0, len = classes.length; i < len; i++) {
            output.append("\t").append(i + 1).append(": ")
                  .append(classes[i].name()).append("\n");
        }
        output.append(" >> ");
        System.out.print(output.toString());
        int playerChoice = in.nextInt();
        in.close();
        switch (playerChoice) {
            case 1:
                playerClass = BaseClass.Barbarian;
                break;
            case 2:
                playerClass = BaseClass.Cleric;
                break;
            case 3:
                playerClass = BaseClass.Mage;
                break;
            case 4:
                playerClass = BaseClass.Fighter;
                break;
        }
    }

    public BaseClass getPlayerClass() {
        return playerClass;
    }

    public static void main(String[] args) {
        Player p = new Player();
        p.setPlayerClass();
        System.out.println("Player selected: " + p.getPlayerClass().name());
    }
}

enum BaseClass {
    Barbarian, Cleric, Mage, Fighter;
}

http://ideone.com/IZNykB

NOTE: To further clarify, you have to watch where you scope variables in Java. The scope the variable belongs to the block of code in which it defined. A block of code being code wrapped in {} braces. Since you originally declared Player_Class inside of a switch, it belonged to the scope of the switch and did not exist outside of it, hence why the method that accessed it didn't return what you wanted it to. When you moved the declaration of Player_Class out of the switch, but still inside of setPlayerClass all you did was scope it to the entire function setPlayerClass but setPlayerClass and getPlayerClass do not share scope and therefore you were still not getting the result you were looking for.

In my above example I've solved this by using a private member variable on the Player class called playerClass that I can assign and return from multiple points within the class because the variable is scoped to the instance I created of the Player class.

于 2013-04-30T15:43:57.160 回答
1

this should work (no need for a switch block):

Player_Class = Base_Class.values()[Base_Class_Choice - 1];
于 2013-04-30T14:35:12.833 回答
0

This is not how you use enums. Enums are fixed, stateless objects, which you would never instantiate (that is why their constructors are private, because the compiler does it for you.

Also, on an aside - Use capital letters for class names, and lower case for fields and methods. Also, use camelCase, not underscore_case - that's how most all Java programmers do it and doing it that way will make it easier for you to read others code and for others to read your code.

I would put BaseClass into a separate file so you can use it everywhere. Then just use the convenient values() array. Here is how I would do it:

BaseClass.java

public enum BaseClass {
    BAR ("Barbarian"), BRD ("Bard"), CLR ("Cleric"), DRU ("Druid"),
    FGT ("Fighter"), MON ("Monk"), PAL ("Paladin"), RAN ("Ranger"),
    ROG ("Rogue"), SOR ("Sorceror"), WIZ ("Wizard");

    private String fullName;
    private BaseClass(String fullName) { this.fullName = fullName; }

    public String getFullName() {
        return fullName;
    }
    // Here you can add other useful methods
};

Player.java

// snip

public void setBaseClass() {
    do {
        System.out.println("Available Classes: ");
        System.out.println("Please select a cooresponding number for class.");
        int i = 1;
        for(BaseClass value: BaseClass.values()){
            System.out.println(i+": "+value.name());
            i++;
        }
        try {
            int baseClassChoice = user_input.nextInt();
            if (i < 1 || i >= BaseClass.values()) {
                System.out.println("That is not a valid choice. Please try again");
                continue;
            }
            Player_Class = BaseClass.values()[i-1];
        } catch (Exception e) {
            System.out.println("You must choose a valid class. Try numbers.");
            user_input.next();
        }
    } while (Base_Class_Choice == 0);
}
于 2013-04-30T14:42:40.873 回答
0

You could use something similar to below sample code. it runs properly in my environment, you could try yourself.

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

enum BaseClass {
    Barbarian(1), Bard(2), Cleric(3), Druid(4), Fighter(5), Monk(6), Paladin(7), Ranger(8), Rogue(9), Sorcerer(10), Wizard(11);

    private int numericValue;
    private static final Map<Integer, BaseClass> intToEnum = new HashMap<Integer, BaseClass>();
    static {
        for (BaseClass type : values()) {
            intToEnum.put(type.getNumericValue(), type);
        }
    }

    private BaseClass(int numericValue)
    {
        this.numericValue = numericValue;
    }

    public int getNumericValue()
    {
        return this.numericValue;
    }

    public static BaseClass fromInteger(int numericValue)
    {
        return intToEnum.get(numericValue);
    }
};

public class Game {
    public static void main(String[] args) throws IOException
    {
        Scanner input = new Scanner(System.in);
        BaseClass choice;

        do {
            System.out.println("Available Classes: ");
            System.out.println("Please select a cooresponding number for class.");

            for (Base_Class value : Base_Class.values()) {
                System.out.println(value.getNumericValue() + " : " + value);
            }

            choice = BaseClass.fromInteger(input.nextInt());
            if (choice == null) {
                System.out.println("Please select a valid class");
            }
        } while (choice == null);

        System.out.println("Successfully chosen: " + choice);
        input.close();
    }
}
于 2013-04-30T15:37:11.047 回答