java - Convert String to the name of a array in another class -
i'm amateur programmer , i'm wondering if it's possible solve problem in specific way.
public class stats { public static int[] hero = {20, 20, 10, 10, 10}; public static int[] villain = {20, 20, 10, 10, 10}; public static void name(int[] name) { //if insert array print values system.out.println("health: " + name[1] + "/" + name[0]); system.out.println("damage: " + name[2]); system.out.println("defense: " + name[3]); system.out.println("agility: " + name[4]); return; }
this want use , if call on using 'hero' or 'villain' works fine.
public class userinterface extends jpanel implements mouselistener, mousemotionlistener { //a lot of code in between not of notice right @override public void mouseclicked(mouseevent e) { if (" ".equals(chess.board[e.gety() / squaresize][e.getx() / squaresize]) == false) { //checks if pressed on legit space on map string name = (chess.board[e.gety() / squaresize][e.getx() / squaresize]); //in scenario 'name' string called "hero" system.out.println("name: " + name); //will write out 'name: hero' stats.name(stats.hero); //works fine stats.name(stats.name); //does not work } }
now want call on right array depending on clicked. moment have arrays called 'hero' , 'villain' in end there surly on 50+ arrays choose on map. writing stats.name(stats.name); not work cause name string , wants int[].
but there way to use string 'name' in such way can call on int[] array named same thing in different class?
i think you're trying object name. can't literally way have, it's common , don't have add code.
let's have rpg game , have team members, each little computer person identified name.
map<string,rpgperson> team = new hashmap<>();
here have way map string (the name) object want (the 1 stats). i'm using suggestion above make "person" object because better using array.
here's quick example:
public class rpgpersonexample { public static void main( string[] args ) { map<string, rpgperson> team = new hashmap<>(); team.put( "sgt. mayhem", new rpgperson( 100, 10, 0 ) ); team.put( "wizard", new rpgperson( 100, 1, 10 ) ); team.put( "dragon", new rpgperson( 1000, 100, 100 ) ); system.out.println( team.get( "dragon" ) ); system.out.println( team.get( "wizard" ) ); } } class rpgperson { private int hits; private int strength; private int magic; public rpgperson( int hits, int strength, int magic ) { this.hits = hits; this.strength = strength; this.magic = magic; } @override public string tostring() { return "rpgperson{" + "hits=" + hits + ", strength=" + strength + ", magic=" + magic + '}'; } }
Comments
Post a Comment