lundi 25 avril 2016

Could this code be more efficient? (If-statement, checking for different inputs)

I am completely new to Java and programming a chatbot. I was wondering; is there a more efficient way to scan for commands then this?

if (msgBody.contains(".command1")) {
cmdCommand1();
} else if (msgBody.contains(".command2")) {
cmdCommand2();
} else if (msgBody.contains(".command3")) {
cmdCommand3();
} else if (msgBody.contains(".command4")) {
cmdCommand4();
} 

etc.. the more commands I add in, the more ridiculous the list gets. I was wondering if there is a more efficient way to do this sort of thing, something that will take up less space.

Aucun commentaire:

Enregistrer un commentaire