lundi 8 juin 2015

Unexpected result with nested if statements in java

I am trying to write some code where there is a textview with a looping set of numbers (0-9) and the user clicks to match the current number shown on screen with the next number in an array. If clicked when showing the correct number, a new textview with another loop is created next to the previous one and you would click to match the next number in the array and so on. If you fail to click when the correct number is onscreen, it's game over.

I managed to come up with the code below but it isn't working how I expected and I'm not sure why. At the moment, on every click it goes straight to game over, and if I only use one 'else' I can never get it to game over unless I get the first click wrong. Does anyone know why this is happening and how I can fix it? I was under the impression that putting 'ifs' and 'elses' within another if would mean that these would only be applicable if the outer 'ifs' condition is met. I'm kind of lost on how else to do this as I would like check 50+ ints so if it doesn't work with 2 then I'm doomed :(

Relevant code:

public class MainActivity extends Activity {

private int a = 0;
private int b = 0;
TextView textView;
TextView2 textView2;
LinearLayout container;
RelativeLayout relativeLayout;
private Handler handler = new Handler();

public Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        updateText;
        a = ++a % 10;
        handler.postDelayed(this, 1000)
    }
};

public Runnable myRun2 = new Runnable() {
    @Override
    public void run() {
        updateText2;
        b = ++b % 10;
        handler.postDelayed(this, 900)
    }
};

public int aNums[] = { 1, 4, 8, 5, 0, 2, 7, 8, 6... };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView);
    loop();
    relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    relativeLayout.setOnClickListener(handler2);
    container = (LinearLayout) findViewById(R.id.linearLayout);
}

View.OnClickListener handler2 = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        textView2 = new TextView(getApplicationContext());
        textView2.setTextAppearance(getApplicationContext(), R.style.textViewStyle);
        textView2.setOnClickListener(this);
        if (a == aNums[0]) {
            stop();
            loop2();
            container = (LinearLayout) findViewById(R.id.linearLayout);
            container.addView(textView2);
            if (b == aNums[1]) {
                textView3 = new TextView(getApplicationContext());
                textView3.setTextAppearance(getApplicationContext(), R.style.textViewStyle);
                textView3.setOnClickListener(this);
                stop2();
                loop3();
                container.addView(textView3);
                // Ideally would add more if statements in the same way from here e.g. if (c == aNums[2]) { etc.
            }
            else { //<--If I take this else out, I can't get game over.
                gameOver(); 
            }
        }
        else {
            gameOver();
        }
    }
};

// Relevant methods:
    public void stop() {
        super.onStop();
        handler.removeCallbacks(myRunnable);
    }

    public void stop2() {
        super.onStop();
        handler.removeCallbacks(myRun2);
    }

    public void loop() {
        handler.post(myRunnable);
    }

    public void loop2() {
        handler.post(myRun2);
    }

    public void loop3() {
        handler.post(myRun3);
    }

    public void updateText() {
        textView.setText("" + a);
    }

    public void updateText2() {
        textView2.setText("" + b);
    }

    public void gameOver() {
        textView.setText("Game Over");
    }
}

Also if anyone knows a better way of checking a looping set of ints against an array then please let me know, I feel like I'm doing it in a roundabout way...

Many thanks.

Aucun commentaire:

Enregistrer un commentaire