我的EditText不会为我的分数添加一个分数。 Android测验应用

时间:2016-07-16 19:23:03

标签: java android android-edittext radio-group

为什么我的程序在提交正确答案时不会为我的分数添加分数。 请帮忙。 以下是我的变量:

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();
    }


}

3 个答案:

答案 0 :(得分:0)

在布局文件中创建一个按钮,如:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Submit"
  android:onClick="calcScore"
  android:id="@+id/button"/>

现在在主要活动中使用calcScore方法编写代码,如:

public void calcScore(View v)
{
    int mscore ;
String q5R = "ripken";
String q5CR = "cal ripken";
String q5Cal = "cal";



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;
    }
}

答案 1 :(得分:0)

您需要先初始化int mscore。

// initialize
int mscore = 0; 

// than add it
mscore += 1;

答案 2 :(得分:0)

我不确定出了什么问题。但它现在似乎工作得很好。我感谢大家花时间帮忙。