Jump to content

How can I display all the cases in a switch statement?

darth_bubbles

So I am building a text adventure. I don't really have any experience programming at all. I want to set it up so when I type "help" it will display all cases in the switch statement.

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

what you'd do is have the switch states seperate from the cases, I'm not sure what language you're trying to get this done in but if it is in java then you can use enum to store the states in and instead of hardcoding the states in you could load it from the enum.

 

When you moved away from hardcoded states you could just list from the enum instead by going through them and outputing them.

Link to comment
Share on other sites

Link to post
Share on other sites

what you'd do is have the switch states seperate from the cases, I'm not sure what language you're trying to get this done in but if it is in java then you can use enum to store the states in and instead of hardcoding the states in you could load it from the enum.

 

When you moved away from hardcoded states you could just list from the enum instead by going through them and outputing them.

Yes it is java. I have no idea what enum is but I will research. Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

what you'd do is have the switch states seperate from the cases, I'm not sure what language you're trying to get this done in but if it is in java then you can use enum to store the states in and instead of hardcoding the states in you could load it from the enum.

 

When you moved away from hardcoded states you could just list from the enum instead by going through them and outputing them.

I'm a bit confused with what you mean. could you write a quick example where the options are "help", "move", and "look at"? Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

what you'd do is have the switch states seperate from the cases, I'm not sure what language you're trying to get this done in but if it is in java then you can use enum to store the states in and instead of hardcoding the states in you could load it from the enum.

 

When you moved away from hardcoded states you could just list from the enum instead by going through them and outputing them.

 

It doesn't need to do anything fancy. just print what you type or something.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm a bit confused with what you mean. could you write a quick example where the options are "help", "move", and "look at"? Thanks.

	public enum MethodCase {		HELP("help"),		MOVE("move"),	        LOOK_TO("look to");				private final String text;				private MethodCase(String text){			this.text = text;		}			    // Change to override 	    @O-V-E-R-I-D-E 	    public String toString() {	        return text;	    }    }    public void methodThatManagesMethods() {        MethodCase methods = getMethod("The string send from the chat or wherever");                switch (methods) {            case HELP:            // Manage Help            break;        }    }        public MethodCase getMethod(String str) {        String calledMethod = str.substring(0, str.indexOf(" "));        // Manage and set the state her        // e.g return HELP if string from HELP is chosen        return null;    }

And somewhere you could have a for loop which goes through the enum and echos it out.

for (MethodCase Method : MethodCase.valus() ) {    System.out.println(Method.toString());}
Link to comment
Share on other sites

Link to post
Share on other sites

	public enum MethodCase {		HELP("help"),		MOVE("move"),	        LOOK_TO("look to");				private final String text;				private MethodCase(String text){			this.text = text;		}			    // Change to override 	    @O-V-E-R-I-D-E 	    public String toString() {	        return text;	    }    }    public void methodThatManagesMethods() {        MethodCase methods = getMethod("The string send from the chat or wherever");                switch (methods) {            case HELP:            // Manage Help            break;        }    }        public MethodCase getMethod(String str) {        String calledMethod = str.substring(0, str.indexOf(" "));        // Manage and set the state her        // e.g return HELP if string from HELP is chosen        return null;    }

And somewhere you could have a for loop which goes through the enum and echos it out.

for (MethodCase Method : MethodCase.valus() ) {    System.out.println(Method.toString());}

 

Thanks! I will go through it and figure out all the parts of it but I'm not sure if my newby brain will understand it all. I will ask you when I don't understand something.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks! I will go through it and figure out all the parts of it but I'm not sure if my newby brain will understand it all. I will ask you when I don't understand something.

just look up "java enum state" on google and I'm pretty sure that you'll find many better examples :P

Link to comment
Share on other sites

Link to post
Share on other sites

just look up "java enum state" on google and I'm pretty sure that you'll find many better examples :P

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×