How do I change a value of a variable within an if in java?

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 not create a new variable within if or change a variable already declared 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();} 

In the above situation, it does not work for obvious reasons, as it will be terminated once the if is finished.

So, someone with a little more knowledge than me, can you explain a solution so that I can select one of the characters?

    
asked by anonymous 13.03.2016 / 01:24

3 answers

1

In fact Saulo Vitor, I believe that the eclipse is complaining for a reason, what would happen if your menu was not any of the options?

I know you should be limiting the options with all the existing ones, but eclipse does not know this, so it complains.

Jogador Player2;

if (menu==1){Player2 = new Ryu();}
else if (menu==2){Player2 = new Blanka();}
else if (menu==3){Player2 = new Zangief();}
else if (menu==4){Player2 = new ChunLi();}
else if (menu==5){Player2 = new Ken();}  

Let's say your menu gets a value of 6. What would happen to your code?

When the value was 6, it would not go through any of the options and when you tried to use Player2, you would receive a large NullPointerException .

For this you have two options:

Jogador Player2 = new Personagem();

if (menu==1){Player2 = new Ryu();}
else if (menu==2){Player2 = new Blanka();}
else if (menu==3){Player2 = new Zangief();}
else if (menu==4){Player2 = new ChunLi();}
else if (menu==5){Player2 = new Ken();}  

This would make your next action with a generic character if the option was not there.

Or use this code, which makes the most sense to me:

Jogador Player2;

if (menu==1){Player2 = new Ryu();}
else if (menu==2){Player2 = new Blanka();}
else if (menu==3){Player2 = new Zangief();}
else if (menu==4){Player2 = new ChunLi();}
else {Player2 = new Ken();}  

So you are deducing that if it is not any of the values, it will certainly be 5 and so the variable will always be initialized.

    
13.03.2016 / 10:59
2

You're almost right. It is a middle ground between the two. It seems that the solution would be this. I just do not guarantee it because I do not have the whole context. You may have a better way to do this, but I can not say just by looking at this passage.

Jogador Player2;

if (menu==1){Player2 = new Ryu();}
else if (menu==2){Player2 = new Blanka();}
else if (menu==3){Player2 = new Zangief();}
else if (menu==4){Player2 = new ChunLi();}
else if (menu==5){Player2 = new Ken();}   
    
13.03.2016 / 01:29
1

This happens because you are trying to assign a type without instantiating it.

Declare the variable outside and create an instance within the condition.

Jogador Player2;

if (menu == 1) { 
    Player2 = new Ryu; 
} else if (menu == 2) {
    Player2 = new Blanka;
} else if (menu == 3) {
    Player2 = new Zangief;
} else if (menu == 4) {
    Player2 = new ChunLi;
} else if (menu == 5){
    Player2 = new Ken;
}
    
13.03.2016 / 01:29