0
votes

I wanted to know if there is any method to store if else condition in java? What I mean is like having a variable to represent the condition. This is my original code

private OnClickListener click2 = new OnClickListener() { 

            @Override
            public void onClick(View v) {
            tv1.setText("");
            List<Integer> mClickedButtonIds = new ArrayList<Integer>();

            int[] mDesiredOrder = new int[] { ans1.getId(), ans2.getId(), ans3.getId(),
                                              ans4.getId(), ans5.getId() }; 

            mClickedButtonIds.add(v.getId());
            if (mClickedButtonIds.size() >= mDesiredOrder.length )

            {            
                if (mClickedButtonIds.get(0) == mDesiredOrder[0] 
                    && mClickedButtonIds.get(1) == mDesiredOrder[1] 
                    && mClickedButtonIds.get(2) == mDesiredOrder[2]
                    && mClickedButtonIds.get(3) == mDesiredOrder[3]
                    && mClickedButtonIds.get(4) == mDesiredOrder[4]
                    )
                {
                    tv1.setText("Correct!");
                }
                else 
                {
                    tv1.setText("Try Again!");
                }
                mClickedButtonIds.clear();
             }
        }
 };

I plan to change it to something like this

private OnClickListener click2 = new OnClickListener() { 

            @Override
            public void onClick(View v) {
            tv1.setText("");
            List<Integer> mClickedButtonIds = new ArrayList<Integer>();

            int[] mDesiredOrder = new int[] { ans1.getId(), ans2.getId(), ans3.getId(),
                                              ans4.getId(), ans5.getId(), ans6.getId() };   

            switch (main)
            {
            case 4 : Variable x = mClickedButtonIds.get(0) == mDesiredOrder[0] 
                    && mClickedButtonIds.get(1) == mDesiredOrder[1] 
                    && mClickedButtonIds.get(2) == mDesiredOrder[2]
                    && mClickedButtonIds.get(3) == mDesiredOrder[3];

            case 5 : Variable x = mClickedButtonIds.get(0) == mDesiredOrder[0] 
                    && mClickedButtonIds.get(1) == mDesiredOrder[1] 
                    && mClickedButtonIds.get(2) == mDesiredOrder[2]
                    && mClickedButtonIds.get(3) == mDesiredOrder[3]
                    && mClickedButtonIds.get(4) == mDesiredOrder[4];

            case 6: Variable x = mClickedButtonIds.get(0) == mDesiredOrder[0] 
                    && mClickedButtonIds.get(1) == mDesiredOrder[1] 
                    && mClickedButtonIds.get(2) == mDesiredOrder[2]
                    && mClickedButtonIds.get(3) == mDesiredOrder[3]
                    && mClickedButtonIds.get(4) == mDesiredOrder[4]
                    && mClickedButtonIds.get(5) == mDesiredOrder[5];
            }

            mClickedButtonIds.add(v.getId());
            if (mClickedButtonIds.size() >= mDesiredOrder.length )

            {            
                if (x)

                {
                    tv1.setText("Correct!");
                }
                else 
                {
                    tv1.setText("Try Again!");
                }
                mClickedButtonIds.clear();
             }
        }
 };

The Variable x is something which I would like to ask. Is there any method to do so or is there any variable that can store if else condition. Cause the original code, it is fixed to 5 clicks. Now I want the number of required clicks to change according to how many clicks the user want.

2
Does x have known inputs and a known subset of conditions? - Richard Tingle
What do you mean "variable that can store if else condition"? This sentence make me stress. - Maroun
or is x just a boolean? - Richard Tingle
You can use SharedPreferences to save a variable value to the "disk". So you can reload it on next run. - Phantômaxx
I think you need to use for statement and move your comparison code to a method, parametrized by click count - hoaz

2 Answers

3
votes

Based on the code snippet, consider a loop:

boolean result = true;
for (int i = 0; i < main; ++i) {
    result = result && mClickedButtonIds.get(i) == mDesiredOrder[i];
    if (!result)
        break; // short-circuit out from loop if false
}

// now you can use "result" to test whether the condition matched all "main" ids
if (result) {
    // correct
} else {
    // bzzt, try again
}
0
votes

If I understand correctly, that you want x to be a condition that you may change programatically (but which conforms to some structure) then you can do this using an interface Question and classes which implement that interface

public interface Question {
    boolean getResponse(String condition1, int condition2);
}

public class StringIsLongCondition implements Question{

    public boolean getResponse(String condition1, int condition2) {
        return condition1.length()>condition2;
    }

}

public class StringIsShortCondition implements Question{

    public boolean getResponse(String condition1, int condition2) {
        return condition1.length()<condition2;
    }

}

Then use like

Question x;

//some code that selects the question
x= new StringIsShortCondition();

if(x.getResponse(someString, someInt){
     //do something
}