无线电组,计算答案两次

radio group, calculating answers twice

我的问题必须有一个简单的答案,我确定我遗漏了一些明显的东西...有人可以为我指出吗?

当我 运行 我 phone 上的应用程序时,无线电组表现正常(这意味着错误答案的分数为 0,正确答案的分数为 1)但是如果我点击错误答案, 然后 返回 到正确答案并重新提交,它会加一分。所以同样的问题只要点击正确答案两次就会显示两分...

package com.example.android.englishquizl3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import static com.example.android.englishquizl3.R.id.radioButton2;
import static com.example.android.englishquizl3.R.id.radioButton3;

public class MainActivity extends AppCompatActivity {

int baseScore = 0;
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Radio Group and attach click handler
    radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    radioGroup1.clearCheck();
    radioGroup2.clearCheck();

    // Attaches checkedChangeListener to radio group
    radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton your = (RadioButton) findViewById(radioButton2);

            if (your.isChecked()) {
                baseScore = baseScore + 1;
            } else {
            }

            int score = 1;
            if (your.isChecked()) score += 1;

            switch (score) {
                case R.id.radioButton2:
                    if (your.isChecked()) ;
                    score++;
                default:
                    break;
            }
        }
    });

    radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton theyre = (RadioButton) findViewById(radioButton3);

            if (theyre.isChecked()) {
                baseScore = baseScore + 1;
            } else {
            }

            int score = 1;
            if (theyre.isChecked()) score += 1;

            switch (score) {
                case R.id.radioButton3:
                    if (theyre.isChecked()) ;
                    score++;
                default:
                    break;
            }
        }
    });

}

public void onSubmit(View v) {
    RadioButton rb1 = (RadioButton) radioGroup1.findViewById(radioGroup1.getCheckedRadioButtonId());
    RadioButton rb2 = (RadioButton) radioGroup2.findViewById(radioGroup2.getCheckedRadioButtonId());
    Toast.makeText(MainActivity.this, "Score: " + baseScore, Toast.LENGTH_SHORT).show();
}
}

还有我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="1) I think _______ cat is lovely."
    android:textColor="#000000" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="you're" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your" />
</RadioGroup>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="2) _______ going to shop tomorrow."
    android:textColor="#000000" />

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="They're" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Their" />
</RadioGroup>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onSubmit"
        android:text="Submit" />

</LinearLayout>

你只是在 baseScore 上加 1,所以每次你改变状态并得到正确答案时,你都会再加 1。如果您检查错误的数字或将其设置为 1

,则必须减少 baseScore

@Emily 你的问题在 switch case 中有很多错误,你在 if 条件中添加了分号,我不知道为什么,因为在这种情况下使用这样的 if 语句是没有意义的。

并且在检查更改时,您正在添加到一个已经有分数 1 的变量。

我的建议是你最好使用一个全局变量来跟踪分数。