0

I have a few issues with what I'm trying to do, but the most pressing is this:

I have a button that sets a hidden random digit, and the user has to attempt to figure out what that digit is. My current code is as follows:

//setting the variables

int max = 9;
int min = 0;
int diff = max-min;
int getNum1;
int a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guess);

//assigning the Java to XML

final TextView urNum1 = findViewById(R.id.txtUrNum1);
final TextView myNum1 = findViewById(R.id.txtMyNum1);
ImageButton up1 = findViewById(R.id.btnUp1);
ImageButton dn1 = findViewById(R.id.btnDn1);
Button chk = findViewById(R.id.btnCheck);
ImageButton start = findViewById(R.id.btnStartGuess);

//starting the randomizer

start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Random rn = new Random();
        urNum1.setText("?");
        a = rn.nextInt(diff+1);
        a += min;
    }
});

//checking for a match

chk.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (a == 0 && getNum1 == 9) {
            urNum1.setText("0"); }
        else if (a == 1 && getNum1 == 0) {
            urNum1.setText("1"); }
        else if (a == 2 && getNum1 == 1) {
            urNum1.setText("2"); }
        else if (a == 3 && getNum1 == 2) {
            urNum1.setText("3");  }
        else if (a == 4 && getNum1 == 3) {
            urNum1.setText("4"); }
        else if (a == 5 && getNum1 == 4) {
            urNum1.setText("5"); }
        else if (a == 6 && getNum1 == 5) {
            urNum1.setText("6"); }
        else if (a == 7 && getNum1 == 6) {
            urNum1.setText("7"); }
        else if (a == 8 && getNum1 == 7) {
            urNum1.setText("8"); }
        else if (a == 9 && getNum1 == 8) {
            urNum1.setText("9"); }

//incrementing the guessed number

up1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getNum1 = Integer.parseInt(myNum1.getText().toString());
                if (getNum1 == 0) {
                    myNum1.setText("1"); }
                else if (getNum1 == 1 ) {
                    myNum1.setText("2"); }
                else if (getNum1 == 2) {
                    myNum1.setText("3"); }
                else if (getNum1 == 3) {
                    myNum1.setText("4"); }
                else if (getNum1 == 4) {
                    myNum1.setText("5"); }
                else if (getNum1 == 5) {
                    myNum1.setText("6"); }
                else if (getNum1 == 6) {
                    myNum1.setText("7"); }
                else if (getNum1 == 7) {
                    myNum1.setText("8"); }
                else if (getNum1 == 8) {
                    myNum1.setText("9"); }
                else if (getNum1 == 9) {
                    myNum1.setText("0"); }
                });

//decrementing the guessed number

dn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getNum1 = Integer.parseInt(myNum1.getText().toString());
                if (getNum1 == 0) {
                    myNum1.setText("9"); }
                else if (getNum1 == 9 ) {
                    myNum1.setText("8"); }
                else if (getNum1 == 8) {
                    myNum1.setText("7"); }
                else if (getNum1 == 7) {
                    myNum1.setText("6"); }
                else if (getNum1 == 6) {
                    myNum1.setText("5"); }
                else if (getNum1 == 5) {
                    myNum1.setText("4"); }
                else if (getNum1 == 4) {
                    myNum1.setText("3"); }
                else if (getNum1 == 3) {
                    myNum1.setText("2"); }
                else if (getNum1 == 2) {
                    myNum1.setText("1"); }
                else if (getNum1 == 1) {
                    myNum1.setText("0"); }
            }
        });

The problem I am running into is that when up1 is pressed, the numbers tend to match up when I check them, but when I press dn1 to get to the matched number, my chosen number is lower than the matched number by two. It may be because my coding is convoluted, and I could probably make it better with using ++ incrementing, but I can't seem to get that to work right either.

Ptolemy Q
  • 3
  • 2

1 Answers1

0

I have figured out the solution on my own, if anyone has run into this issue as well. In the code where I change the myNum value by incrementing/decrementing the value, I'm only checking the current value for each condition of the 'if' statement without also changing the value within. So no matter how many times I check it, it will always be one off. My fix:

getNum1 = Integer.parseInt(myNum1.getText().toString());
    if (getNum1 == 0) {
        myNum1.setText("9");
        getNum1 = 9; }
    else if (getNum1 == 9 ) {
        myNum1.setText("8");
        getNum1 = 8; }
    else if (getNum1 == 8) {
        myNum1.setText("7");
        getNum1 = 7; }

And so on from there.

Ptolemy Q
  • 3
  • 2