jeudi 20 octobre 2016

Using an arraylist for multiple if conditions

I'm currently working on a school project in Android Studio and so far I've written a code which generates a random equation. Here is the code which gereates the random equation:

String[] operationSet = new String[]{"+", "-", "/", "*"};
String stringResultOfEquation;
String equation;
double doubleAnswer1;
public void start1() {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation += numbers.get(i);
        equation += operations.get(i);
    }
    equation += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView3);
    TextEquation.setText(equation);

    String stringResultOfEquation = String.valueOf(equation);

    double doubleAnswer1 = eval(stringResultOfEquation);

    String stringAnswer = Double.toString(doubleAnswer1);

    TextView textAnswer = (TextView)findViewById(R.id.textView4);
    textAnswer.setText(stringAnswer);
}

Now the app displays two equations on the screen and the user then has to dicide wether the second equation has a bigger or smaller result than the first one. If the second equation is bigger, the user has to do a swipemotion UP and if the second equation is smaller, the user has to do a swipemotion DOWN. However I don't know how to write the code for this. I tried this:

if((doubleAnswer1 > doubleAnswer2) && (MotionEvent.ACTION_DOWN = true)){
    //create new equation
    }

but that didn't work. It told me that "Operator && can't be applied to boolean, int"

Now I'm curious if I could use an arraylist like this:

if(doubleAnswer1 > doubleAnswer2){
        // put "smaller" to arraylist
    } else {
        // put "bigger" to arraylist
    }

    if(MotionEvent.ACTION_DOWN = true){
        // put "smaller" to arraylist
    } 
    if(MotionEvent.ACTION_UP = true){
        // put "bigger" to arraylist
    }

Then the code would check the arraylist if the elemnts of the arraylist are the same. If so, the next equation will be generated. If the elemnts of the arraylist are different, the process will be stopped. I really don't know if that would work, so could someone maybe tell me if? Or is there an other way to solve my problem?

If anything is 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