How to change a value of a variable inside an if in java?

Asked

Viewed 1,406 times

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 much if, but as it is not quite the problem of the question, it is the tip as comment same.

  • If the answer really accepts is the one that solved the problem, then the question was wrong. The error shown indicates something else.

3 answers

2

You almost got it right. It’s a middle ground between the two. It seems that the solution would be this. I just don’t guarantee it because I don’t have the whole context. You may have an even better way of doing this, but I can’t say just seeing this stretch.

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(); }

I put in the Github for future reference.

1

This happens because you are trying to assign a type without instance 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;
}

1


In fact Saul Victor, I believe the eclipse is complaining for a reason, which would happen if your menu were not any of the options?

I know you must be limiting the options with all the existing ones, but the eclipse doesn’t know that, so he 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 assume your menu is set to 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 great 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 cause your next action to be with a Generic character if the option was not there.

Or use that code, which makes more 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 definitely be 5 and so the variable will always be initialized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.