2

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

}

ata
  • 3,398
  • 5
  • 20
  • 31

2 Answers2

2

You set int lives = 3; in your method. So at every click it is 3, and you count up or down. You have to keep lives in your class, outside the method. For example, put it just below final Button rollButton;.

Business Tomcat
  • 1,021
  • 9
  • 12
2

Whenever user will click on rollButton lives will be displayed as 2(if decremented) else 4(if incremented), reason behind this is you declare and initialize the live variable into the method onClick, so every time user click on button lives will be displayed either 2 or 4.

In solution of this problem declare and initialized the variable outside the method(On class level).

ash9
  • 149
  • 1
  • 3