K
K
Kirill Morozov2016-09-29 11:58:44
Java
Kirill Morozov, 2016-09-29 11:58:44

How to fix the program execution logic?

Hello! Please help me figure it out...
I'm writing a Java testing program for myself. In the test, it is necessary to implement the function of several correct answers (checkboxes). However, something is always wrong. Now, for example, when checking, all answer options are immediately counted as correct (checkBox1Status = true;).

public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.checkBox:
                if (checkBox1.isChecked() == true && (checkBox1.getText().equals(writeAnswer1) ||
                        checkBox1.getText().equals(writeAnswer2) ||
                        checkBox1.getText().equals(writeAnswer3) ||
                        checkBox1.getText().equals(writeAnswer4)))

            {
                    checkBox1Status = true;
                }
                else if((checkBox1.isChecked() == false && !checkBox1.getText().equals(writeAnswer1)) ||
                        (checkBox1.isChecked() == false && !checkBox1.getText().equals(writeAnswer2)) ||
                        (checkBox1.isChecked() == false && !checkBox1.getText().equals(writeAnswer3)) ||
                        (checkBox1.isChecked() == false && !checkBox1.getText().equals(writeAnswer4))
                        )

                {
                    checkBox1Status = true;
                }
                else if((checkBox1.isChecked() == true && !checkBox1.getText().equals(writeAnswer1)) ||
                        (checkBox1.isChecked() == true && !checkBox1.getText().equals(writeAnswer2)) ||
                        (checkBox1.isChecked() == true && !checkBox1.getText().equals(writeAnswer3)) ||
                        (checkBox1.isChecked() == true && !checkBox1.getText().equals(writeAnswer4))
                        )

                {
                    checkBox1Status = false;
                }
                else if((checkBox1.isChecked() == false && checkBox1.getText().equals(writeAnswer1)) ||
                        (checkBox1.isChecked() == false && checkBox1.getText().equals(writeAnswer2)) ||
                        (checkBox1.isChecked() == false && checkBox1.getText().equals(writeAnswer3)) ||
                        (checkBox1.isChecked() == false && checkBox1.getText().equals(writeAnswer4)))

                {
                    checkBox1Status = false;
                }


                if ((checkBox2.isChecked() == true && checkBox2.getText().equals(writeAnswer1)) ||
                        (checkBox2.isChecked() == true && checkBox2.getText().equals(writeAnswer2)) ||
                        (checkBox2.isChecked() == true && checkBox2.getText().equals(writeAnswer3)) ||
                        (checkBox2.isChecked() == true && checkBox2.getText().equals(writeAnswer4))
                        )

                {
                    checkBox2Status = true;
                }
                else if((checkBox2.isChecked() == false && !checkBox2.getText().equals(writeAnswer1)) ||
                        (checkBox2.isChecked() == false && !checkBox2.getText().equals(writeAnswer2)) ||
                        (checkBox2.isChecked() == false && !checkBox2.getText().equals(writeAnswer3)) ||
                        (checkBox2.isChecked() == false && !checkBox2.getText().equals(writeAnswer4))
                        )

                {
                    checkBox2Status = true;
                }
                else if((checkBox2.isChecked() == true && !checkBox2.getText().equals(writeAnswer1)) ||
                        (checkBox2.isChecked() == true && !checkBox2.getText().equals(writeAnswer2)) ||
                        (checkBox2.isChecked() == true && !checkBox2.getText().equals(writeAnswer3)) ||
                        (checkBox2.isChecked() == true && !checkBox2.getText().equals(writeAnswer4))
                        )

                {
                    checkBox2Status = false;
                }
                else if((checkBox2.isChecked() == false && checkBox2.getText().equals(writeAnswer1)) ||
                        (checkBox2.isChecked() == false && checkBox2.getText().equals(writeAnswer2)) ||
                        (checkBox2.isChecked() == false && checkBox2.getText().equals(writeAnswer3)) ||
                        (checkBox2.isChecked() == false && checkBox2.getText().equals(writeAnswer4)))

                {
                    checkBox2Status = false;
                }
                if ((checkBox3.isChecked() == true && checkBox3.getText().equals(writeAnswer1)) ||
                        (checkBox3.isChecked() == true && checkBox3.getText().equals(writeAnswer2)) ||
                        (checkBox3.isChecked() == true && checkBox3.getText().equals(writeAnswer3)) ||
                        (checkBox3.isChecked() == true && checkBox3.getText().equals(writeAnswer4))
                        )

                {
                    checkBox3Status = true;
                }
                else if((checkBox3.isChecked() == false && !checkBox3.getText().equals(writeAnswer1)) ||
                        (checkBox3.isChecked() == false && !checkBox3.getText().equals(writeAnswer2)) ||
                        (checkBox3.isChecked() == false && !checkBox3.getText().equals(writeAnswer3)) ||
                        (checkBox3.isChecked() == false && !checkBox3.getText().equals(writeAnswer4))
                        )

                {
                    checkBox3Status = true;
                }
                else if((checkBox3.isChecked() == true && !checkBox3.getText().equals(writeAnswer1)) ||
                        (checkBox3.isChecked() == true && !checkBox3.getText().equals(writeAnswer2)) ||
                        (checkBox3.isChecked() == true && !checkBox3.getText().equals(writeAnswer3)) ||
                        (checkBox3.isChecked() == true && !checkBox3.getText().equals(writeAnswer4))
                        )

                {
                    checkBox3Status = false;
                }
                else if((checkBox3.isChecked() == false && checkBox3.getText().equals(writeAnswer1)) ||
                        (checkBox3.isChecked() == false && checkBox3.getText().equals(writeAnswer2)) ||
                        (checkBox3.isChecked() == false && checkBox3.getText().equals(writeAnswer3)) ||
                        (checkBox3.isChecked() == false && checkBox3.getText().equals(writeAnswer4)))

                {
                    checkBox3Status = false;
                }

                if ((checkBox4.isChecked() == true && checkBox4.getText().equals(writeAnswer1)) ||
                        (checkBox4.isChecked() == true && checkBox4.getText().equals(writeAnswer2)) ||
                        (checkBox4.isChecked() == true && checkBox4.getText().equals(writeAnswer3)) ||
                        (checkBox4.isChecked() == true && checkBox4.getText().equals(writeAnswer4))
                        )

                {
                    checkBox4Status = true;
                }

                else if((checkBox4.isChecked() == false && !checkBox4.getText().equals(writeAnswer1)) ||
                        (checkBox4.isChecked() == false && !checkBox4.getText().equals(writeAnswer2)) ||
                        (checkBox4.isChecked() == false && !checkBox4.getText().equals(writeAnswer3)) ||
                        (checkBox4.isChecked() == false && !checkBox4.getText().equals(writeAnswer4))
                        )

                {
                    checkBox4Status = true;
                }
                else if((checkBox4.isChecked() == true && !checkBox4.getText().equals(writeAnswer1)) ||
                        (checkBox4.isChecked() == true && !checkBox4.getText().equals(writeAnswer2)) ||
                        (checkBox4.isChecked() == true && !checkBox4.getText().equals(writeAnswer3)) ||
                        (checkBox4.isChecked() == true && !checkBox4.getText().equals(writeAnswer4))
                        )

                {
                    checkBox4Status = false;
                }
                else if((checkBox4.isChecked() == false && checkBox4.getText().equals(writeAnswer1)) ||
                        (checkBox4.isChecked() == false && checkBox4.getText().equals(writeAnswer2)) ||
                        (checkBox4.isChecked() == false && checkBox4.getText().equals(writeAnswer3)) ||
                        (checkBox4.isChecked() == false && checkBox4.getText().equals(writeAnswer4)))

                {
                    checkBox4Status = false;
                }

                if (checkBox1Status==true && checkBox2Status==true && checkBox3Status==true && checkBox4Status==true)
                {
                    Toast toast1 = Toast.makeText(getApplicationContext(),
                            "Верно!", Toast.LENGTH_SHORT);
                    toast1.show();

                    Intent intent = new Intent(MultyChoise.this, MainActivity.class);
                    startActivity(intent);
                }

                break;

Tell me, please, what's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-09-29
@Rou1997

Because the code, that is, the code is too long, complex and therefore unreadable, at least, they could &&use nested ones instead if:

if (checkBox1.isChecked() == true) {
  if (...) {
    ...
  }
}

Otherwise, you generally double-check one CheckBox, whether it is true or false.
Although, some are already managed and everything works correctly for them, here is one of them , learn!

P
postflow, 2016-10-25
@postflow

There is a good example of your task in this book
https://www.dropbox.com/s/a45l6lsrepz5b5v/%D0%A5%D...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question