Monday, 15 February 2010

java - How do I access a String from another method? -



java - How do I access a String from another method? -

i'm trying create pokémon type of game test out novice coding skills. have run bit of problem.

if run programme stands, window pops 2 attacks (tackle , thunder shock). tackle works when click once, thunder shock needs clicked twice; i'm guessing because pikaatktype() used in both if's.

i need compare "picked" string method message box have work properly.

my question being, how access "picked" pikaatktype() method , set if statements can compare message box inputs without having press "ok" twice thunder shock. (i in simple way; i'm not best @ coding.)

if (encounter().equals("attack")) { if (pikaatktype().equals("tackle")) { pikadmg = 3; } else if (pikaatktype().equals("thunder shock")) { pikadmg = 4; } }

the method pikaatktype() (remember these pieces of code):

static string pikaatktype() { string[] choices = {"tackle", "thunder shock", "", ""}; string picked = (string)joptionpane.showinputdialog( null, "select attack: ", "attack!", joptionpane.question_message, null, choices, choices[0]); homecoming picked; }

if understand question, 1 solution "attack" 1 time , store in local variable. like,

if (encounter ().equals ("attack")) { string attack = pikaatktype (); if (attack.equals ("tackle")) { pikadmg = 3; } else if (attack.equals ("thunder shock")) { pikadmg = 4; }

java

No comments:

Post a Comment