我有一个简单的测验应用程序,带有5个单选按钮和5个复选框。复选框问题有多个答案,对于这样一个问题,4个复选框中有3个正确答案。当用户在三个框中选择错误的一个时,分数仍然会增加,如何不为错误的选择增加复选框。
MainActivity:
[public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
/**
* The number of correct answers
*/
int score = 0;
public void end_Test(View view) {
EditText nameField = findViewById(R.id.name_field);
String name = nameField.getText().toString();
//Called question checking methods
question_1();
question_2();
question_3();
question_4();
question_5();
question_6();
question_7();
question_8();
question_9();
question_10();
// Display the test result on the screen
String test_result = createTestResult(name, score);
displayResult(test_result);
// Disabled "End Test" button after clicking on it.
Button endTestButton = findViewById(R.id.end_test_button);
endTestButton.setEnabled(false);
}
public void question_10() {
// Figure out if the user chose "Ipad" answer
CheckBox IpadCheckBox = findViewById(R.id.ipad_checkbox);
boolean hasIpad = IpadCheckBox.isChecked();
// Figure out if the user chose "Imac" answer
CheckBox ImacCheckBox = findViewById(R.id.imac_checkbox);
boolean hasImac = ImacCheckBox.isChecked();
// Figure out if the user chose "Ipod" answer
CheckBox IpodCheckBox = findViewById(R.id.ipod_checkbox);
boolean hasIpod = IpodCheckBox.isChecked();
// Figure out if the user chose "Ipod" answer
CheckBox ImaxCheckBox = findViewById(R.id.imax_checkbox);
ImaxCheckBox.isChecked();
if (hasIpad) {
if (hasImac){
if (hasIpod){
increment_score();
}
}
}
}
/**
* This method is called when user selected the correct answer.
* Added +1 to score for each correct answer
*/
private int increment_score() {
score = ++score;
return score;
}
}][1]
答案 0 :(得分:1)
应该有多个答案吗?如果没有,我会将CheckBoxe's
替换为RadioButton's
。最后我只会检查正确的解决方案:
RadioButton rbIPod = findViewById(R.id.imax_checkbox);
if(rbIPod.isChecked()){
increment_score();
}
根据您的评论进行编辑
所以你会这样:
if (hasIpad && hasImac && hasIpod && !hasImax) {
increment_score();
}
在这种情况下,你需要选择Ipad-,Imac-和Ipod CheckBox
来调用increment_score()
-
答案 1 :(得分:0)
你在三个答案都是正确的情况下增加分数,省略第四个答案Imax
,你宁愿做那样的事情:
if (hasIpad)
increment_score();
if (hasImac)
increment_score();
//.....
并为另外两个做同样的事情。
答案 2 :(得分:0)
您在下面写的逻辑是:
if (hasIpad) {
if (hasImac){
if (hasIpod){
increment_score();
}
}
}
这意味着如果HasIpad为true,那么如果HasImac为true,那么如果hasIpod为true,则为Increment score。'
应该是什么:(假设正确的答案是HasImac)
If(HasImac)
{
increment();
}
else
{
you_disappoint_me();
}