java - How do I add a keypress action in my program? -


i making basic java based rpg game many of options go along , want make when ability type, if press "x" automatically quit game. don't want continuously add "if-then" statement every time user progresses.

what don't want do: (i have on 50 times for: inventory, quit game, character information , more)

switch (choice1)   {      case "x":         system.out.println("\nyou quit game!");         system.exit(0);         break;        }     

what have: (doesn't work)

import java.util.scanner; import java.awt.*; import java.awt.event.*;   public class thedungeon extends keyadapter {     public void keypressed(keyevent e) {         char ch = e.getkeychar();         if (ch == 'a')         {         system.out.println("you pressed a");          }         }        public static void main(string[] args)     {     /* variables...      */     system.out.println("e: check experience , level");     system.out.println("c: character information");     system.out.println("i: inventory");     system.out.println("x: quit game");     choice1 = keyboard.nextline();     switch (choice1)         {         case "x":                                         //this section             system.out.println("\nyou quit game!");   //here works             system.exit(0);                               //but don't         break;                                            //want add section     }                                                     //every time user                                                           //progresses. 

to use keyadapters and/or keylisteners need construct gui in add these adapters/listners too.

the way reading in users action valid way console app.

edit extending on blakep's answer if have determineaction method have map of text print out need add special actions keys.

map<character, string> actiontext = new hashmap<character, string>();  actiontext.put('x', "\nyou quit game!"); actiontext.put('i', "\ninventory items:\n  things here");   private void determineaction(char choice) {    system.out.println(actiontext.get(choice));    switch (choice1)    {      case "x":                                                  system.exit(0);                                     break;                                        } } 

or should have method each special action, keep switch shorter , easier read. so

private void determineaction(char choice) {    system.out.println(actiontext.get(choice));    switch (choice1)    {      case "x":                                                  doexit();                                        break;                                          case "i":          printinventory();          break;     } }  private void doexit() {     system.out.println("\nyou quit game!");     system.exit(0); }  private void printinventory() {     system.out.println("\ninventory items:");     system.out.println("\n  things here"); } 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -