【发布时间】:2016-11-19 18:22:35
【问题描述】:
为什么当我提交正确答案时,我的程序没有给我的分数加分。 请帮忙。 这是我的变量:
public class MainActivity extends AppCompatActivity {
int mscore =0 ;
String q5R = "ripken";
String q5CR = "cal ripken";
String q5Cal = "cal";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkResult(View v) {
//Access the RadioGroup view and save it to a variable.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.q1RadioBox);
//Get the id of the RadioButton that is checked and save it
//as an integer variable.
int solutionId = radioGroup.getCheckedRadioButtonId();
//Use if statements to respond based on whether
//it is the id of the correct answer.
if (solutionId == R.id.answer1) {
mscore = mscore +1;
} else {
mscore = mscore + 0;
}
//Access the RadioGroup2 view and save it to a variable.
RadioGroup radioGroup2 = (RadioGroup) findViewById(R.id.q2RadioBox);
//Get the id of the RadioButton that is checked and save it
//as an integer variable.
int solutionId2 = radioGroup2.getCheckedRadioButtonId();
//Use if statements to respond based on whether
//it is the id of the correct answer.
if (solutionId2 == R.id.answer2) {
mscore = mscore +1;
} else {
mscore = mscore + 0;
}
CheckBox ws1966 = (CheckBox) findViewById(R.id.checkbox_1966);
CheckBox ws1983 = (CheckBox) findViewById(R.id.checkbox_1983);
CheckBox ws1970 = (CheckBox) findViewById(R.id.checkbox_1970);
CheckBox ws1984 = (CheckBox) findViewById(R.id.checkbox_1984);
CheckBox ws1961 = (CheckBox) findViewById(R.id.checkbox_1961);
// Check for Required Answers
if (ws1966.isChecked() && ws1983.isChecked() && ws1970.isChecked() && !ws1984.isChecked() && !ws1961.isChecked()) {
mscore = mscore + 3;
}
EditText questionFive = (EditText) findViewById(R.id.question_5);
String questionFiveAnswer = questionFive.getText().toString();
if (questionFiveAnswer.equalsIgnoreCase(q5R) || questionFiveAnswer.equalsIgnoreCase(q5CR) || questionFiveAnswer.equalsIgnoreCase(q5Cal) ){
mscore = mscore + 1;
}
EditText enterName = (EditText) findViewById(R.id.name_entry);
String name = enterName.getText().toString();
Context context = getApplicationContext();
CharSequence text = name + " your score is: " + mscore + " points out of a possible 6 points";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
【问题讨论】:
-
这段代码是在你的activity onCreate()下还是onClick()下
-
公共类 MainActivity 扩展 AppCompatActivity { int mscore ;字符串 q5R = "ripken";字符串 q5CR = "cal ripken";字符串 q5Cal = "cal";
-
我已经对其进行了编辑,因此我的整个代码都在问题的上方。
-
似乎 if 语句由于某种原因没有执行。
-
我的单选按钮和复选框正在为分数添加分数,而不是我的 edittext 问题。
标签: java android android-edittext radio-group