jeudi 20 octobre 2016

Multiple if conditions with swipe events

I'm currently working on a school project in Android Studio and so far I've written a code which generates a random equation. Now I want to display two equations and the user has to decide which of these two has a bigger result. The user then has to do a swipemotion up if the second equation is bigger and a swipemotion down if the second equation is smaller. Here is the code for the random equations:

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);
}

However I don't know how to write the code for the swipe events. I was thinking of multiple if conditions like:

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

And for every other possibilty (doubleAnswer1 < doubleAnswer2) as well if the MotionEvent was made the wrong direction. I tried to do it that way but it didn't work, so I don't know what to try out now. Maybe someone has an idea how to solve this.

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