Java 不要为错误的复选框选择增加分数
我有一个简单的测验应用程序,有5个单选按钮和5个复选框。复选框问题有多个答案,对于这样一个问题,在4个复选框中有3个正确答案。当用户在三个框中选择错误的一个时,分数仍在增加,如何不增加错误选择复选框 主要活动:Java 不要为错误的复选框选择增加分数,java,android,checkbox,Java,Android,Checkbox,我有一个简单的测验应用程序,有5个单选按钮和5个复选框。复选框问题有多个答案,对于这样一个问题,在4个复选框中有3个正确答案。当用户在三个框中选择错误的一个时,分数仍在增加,如何不增加错误选择复选框 主要活动: [public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { /** * The number
[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]
在三个答案正确的情况下增加分数,忽略第四个
Imax
,您更愿意这样做:
if (hasIpad)
increment_score();
if (hasImac)
increment_score();
//.....
对另外两个做同样的事情。应该有多个答案吗?如果没有,我会将
复选框的
替换为单选按钮的
。最后,我只会检查正确的解决方案:
RadioButton rbIPod = findViewById(R.id.imax_checkbox);
if(rbIPod.isChecked()){
increment_score();
}
根据您的评论进行编辑
所以你要这样做:
if (hasIpad && hasImac && hasIpod && !hasImax) {
increment_score();
}
在这种情况下,您需要选中Ipad、Imac和Ipod的
复选框
,以调用increment\u score()
if (hasIpad) {
if (hasImac){
if (hasIpod){
increment_score();
}
}
}
这意味着“如果HasIpad为真,那么HasImac为真,如果HasIpad为真,那么增加分数。”
应该是什么:(假设正确答案是HasImac)
我的问题有多个答案,我想为所有正确答案增加一分。@Rajashekar没问题!:)通过这样做,即使选择了错误的项目复选框,分数也会增加。我不想在错误的项目选择中增加分数。我有这个测验问题的复选框。这个问题有3个多答案,所以当用户选择错误的项目时,我需要停止增加分数。
If(HasImac)
{
increment();
}
else
{
you_disappoint_me();
}