从方法中获得恒定的错误值

时间:2017-05-12 19:26:59

标签: java android

我有3个问题

 private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];

当没有回答所有问题时,我有一个方法

private boolean allAnswersChecked() {
    for (boolean radioAnswer : isAnswered) {
        if (!radioAnswer) {
            return false;
        }
    }
    return true

显示文字"You haven't checked all answers"

问题在于,即使按下所有false,它也会以某种方式返回rbs。即使已经检查了所有"You haven't checked all answers",仍然会显示另一个单词rbs

如何更改上述方法?

或者可能

它发生了这行中的值

static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
不要改变所有人。也许必须在方法setOnCheckedChangeListener中添加一些额外的代码,到目前为止只检查答案是否正确。

以下是rb [0]

的代码
 rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.radioButton1) {
                Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
                checked[0] = true;
                answer[0] = true;
            } else {
                checked[0] = true;
                answer[0] = false;
            }

        }
    });

3 个答案:

答案 0 :(得分:1)

您在哪里更新 isAnswered 数组。在您提供的代码段中,它仅更新答案和已检查的数组。

可能这就是为什么你总是对 isAnswered 数组的所有3个数组元素都是假的。

您在哪里更新 isAnswered 数组。在您提供的代码段中,它仅更新答案和已检查的数组。

可能这就是为什么你总是对 isAnswered 数组的所有3个数组元素都是假的。

嗨罗马,

我相信你有多个无线电组,每组有3个单选按钮。如果是这种情况,只需在上面的代码片段稍作修改就可以了。

rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radioButton1) {
            Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
            checked[0] = true;
            answer[0] = true;
        } else {
            checked[0] = true;
            answer[0] = false;
        }
        // update isAnswered[0] to true. as this gets invoked when you choose any radio button.
        isAnswered[0] = true;
    }
});

如果有帮助,请告诉我。

答案 1 :(得分:1)

试试这个:

static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS]; // initially all false

.............
....................

rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radioButton1) {
            Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
            checked[0] = true;
            answer[0] = true;
        } else {
            checked[0] = true;
            answer[0] = false;
        }

        // Update value as user selected radio button
        isAnswered[0] = true;
    }
});

为您的其他RadioGroupsrg2, rg3, ......)执行相同操作。希望这会有所帮助〜

答案 2 :(得分:0)

我已经实现了一个快速类来验证您的逻辑,并且在您的实现中没有发现任何问题。

public class MainActivity extends AppCompatActivity {

    private static int NUMBER_OF_QUESTIONS = 3;
    static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];

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

    private void setBooleanValues() {
        isAnswered[0]=true;
        isAnswered[1]=false;
        isAnswered[2]=true;
    }

    private void allAnswersChecked() {
        for (boolean radioAnswer : isAnswered) {
            if (!radioAnswer) {
                //return false;
                Log.d("allAnswersChecked","false");
            }
            else {
                Log.d("allAnswersChecked","true");
            }
        }
        //return true;
    }

}

请参阅日志输出:

05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: true
05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: false
05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: true

所以我建议在调用allAnswersChecked()之前打印值并检查它是否正确设置到isAnswered数组中。