1
I created an abstract class called Player and also created the player subclasses.
Now I need to create a menu for the user to choose which character he will use in the battle, but I can’t create a new variable inside the if
or change an already declared variable on the outside.
Jogador Player2;
if (menu==1){Player2 = Ryu;}
else if (menu==2){Player2 = Blanka;}
else if (menu==3){Player2 = Zangief;}
else if (menu==4){Player2 = ChunLi;}
else if (menu==5){Player2 = Ken;}
In the above situation when trying to use the Player2 variable Eclipse says that the variable has not yet been initialized.
if (menu==1){Jogador Player2 = new Ryu();}
else if (menu==2){Jogador Player2 = new Blanka();}
else if (menu==3){Jogador Player2 = new Zangief();}
else if (menu==4){Jogador Player2 = new ChunLi();}
else if (menu==5){Jogador Player2 = new Ken();}
Already in the above situation, it does not work for obvious reason, since it will be closed once the if is finalized.
So, someone with a little more knowledge than me, can explain to me a solution so that I can select one of the characters?
switch...case
would be better than this muchif
, but as it is not quite the problem of the question, it is the tip as comment same.– user28595
If the answer really accepts is the one that solved the problem, then the question was wrong. The error shown indicates something else.
– Maniero