jeudi 30 août 2018

How to use my if/else statement in a for loop with arrays?

I have code that compares userinput from EditText to the correct translation(String[] answers). Now i wrote a if/else statement but I'm trying to put that in a for loop. Otherwise I need to write over 30 statements. just with a few numbers different.

Now I have came up with the following 'basis':

for(int i = 0; i < answers.length; i++){

        if (Uinput[].equals(answers[])) {
            edit1.setBackgroundColor(Color.parseColor("#00FF00"));
        } else {
            edit1.setBackgroundColor(Color.parseColor("#FF0000"));
        }
    }

What do I need to put between the brackets([]) as it wont allow it empty, and further any recommendations or changes? Thanks in advance.

Code sample of what I'm currently using:

private String[] answers = {"daylight", "task"};

private EditText edit1;

this.edit1 = findViewById(R.id.edit1);

String Uinput[] = {edit1.getText().toString()};

//        Comparison 1
        if (Uinput[0].equals(answers[0])) {
            edit1.setBackgroundColor(Color.parseColor("#00FF00"));
        }
        else {
            edit1.setBackgroundColor(Color.parseColor("#FF0000"));
        }

Aucun commentaire:

Enregistrer un commentaire