So I'm trying to add lives in my game on Android, and when I run my if else
block, the lives will only decrement by 1 or add by 1 but won't go any further than just going down or up by one, I need it to go down every time a guess is wrong but it doesn't.
This is the block of code maybe I just need to loop it but I'm not sure how.
int lives = 3;
if(guessNumber.getValue() != number) {
lives--;
Log.d("dicee", "You have " + lives + " lives left");
} else {
lives++;
Log.d("dicee", "You have " + lives + " lives left");
}
All the code is here if any other details are needed
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.NumberPicker;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button rollButton;
rollButton = (Button) findViewById(R.id.rollButton);
//image views for dice
final ImageView leftDice = (ImageView)
findViewById(R.id.image_leftDice);
//Dice array to grab and display images of dice
final int[] diceArray =
{R.drawable.dice1,
R.drawable.dice2,
R.drawable.dice3,
R.drawable.dice4,
R.drawable.dice5,
R.drawable.dice6};
rollButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("dicee", "Ze Button Has Been Pressed!");
Random randomNumberGenerator = new Random();
int number = randomNumberGenerator.nextInt(7);
Log.d("dicee", "The Random Number Is: " + number);
leftDice.setImageResource(diceArray[number - 1]);
NumberPicker guessNumber = null;
guessNumber = (NumberPicker)findViewById(R.id.guessNumber);
guessNumber.setMaxValue(6);
guessNumber.setMinValue(1);
guessNumber.setWrapSelectorWheel(false);
if(guessNumber.getValue() == number) {
Log.d("dicee", "You Are Correct");
} else {
Log.d("dicee", "Try Again");
}
int lives = 3;
if(guessNumber.getValue() != number) {
lives--;
Log.d("dicee", "You have " + lives + " lives left");
} else {
lives++;
Log.d("dicee", "You have " + lives + " lives left");
}
}
});
}
}