Answer the question
In order to leave comments, you need to log in
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;
Answer the question
In order to leave comments, you need to log in
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 (...) {
...
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question