jeudi 20 octobre 2016

Use arraylist for multiple if statements (with buttons)

I'm currently working on a school project in Android Studio and so far I've written a code which generates random equations. Now I display two equations on the screen and the user then has to decide wether the second equation is bigger or smaller than the first one. If the second is bigger, the user presses the button 'bigger', if the second one is smaller, the user presses the button with 'smaller'. Now I'm trying to write the code that if the user pressed correct, it generates a new equation, if he was wrong, the process stops. I was thinking of if statements like so:

final ArrayList<String> arrayListCheck = new ArrayList<String>();

    if(doubleAnswer1 > doubleAnswer2){
        arrayListCheck.add("smaller");
    } else {
        arrayListCheck.add("bigger");
    }

    final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
    final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.equals(buttonBigger)){
                arrayListCheck.add("bigger");
            } else {
                arrayListCheck.add("smaller");
            }
        }
    };

    buttonBigger.setOnClickListener(listener);
    buttonSmaller.setOnClickListener(listener);

In the arraylist arrayListCheck it will store either 'bigger' or 'smaller'. Now I want to check if the elements in the arraylist are both the same (either both 'bigger' or 'smaller'), if so a new equation will be generated. If the elements in the arraylist are different (not both the same), the process will be stopped. I don't know if that really works, so it would be nice if someone could help me with this.

If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)

Thank you already in advance for your help!

Aucun commentaire:

Enregistrer un commentaire