从方法获取恒定的假值 [英] Getting constant false value from method

查看:39
本文介绍了从方法获取恒定的假值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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];

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

and when NOT all questions are answered I have a method

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

显示文字您尚未检查所有答案"

问题是,即使按下所有 rbs ,它仍会以某种方式仅返回 false .即使已检查所有 rbs ,仍会显示文本您尚未检查所有答案" 的其他单词.

The problem is that somehow it returns only false even when all rbs are pressed. Another words text "You haven't checked all answers" is still displayed even when all rbs are already checked.

如何更改上述方法?

或可能

发生此行中的值

static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];

完全不要更改.也许必须向方法 setOnCheckedChangeListener 中添加一些额外的代码,到目前为止,该方法仅检查答案是否正确.

don't change AT ALL. Maybe some extra code must be added to method setOnCheckedChangeListener which so far checks only if answers are correct or not.

这是我的 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;
            }

        }
    });

推荐答案

您要在哪里更新 isAnswered 数组.在您提供的代码段中,它仅更新答案和选中的数组.

Where are you updating isAnswered array. In the code snippet you have provided, it updates only answer and checked arrays.

这也许就是为什么您总是为 isAnswered 数组的所有3个数组元素弄错的原因.

Probably that is why you are always getting false for all 3 array elements of isAnswered array.

您要在哪里更新 isAnswered 数组.在您提供的代码段中,它仅更新答案和选中的数组.

Where are you updating isAnswered array. In the code snippet you have provided, it updates only answer and checked arrays.

这也许就是为什么您总是为 isAnswered 数组的所有3个数组元素弄错的原因.

Probably that is why you are always getting false for all 3 array elements of isAnswered array.

罗马,你好

我相信您有多个带有3个单选按钮的单选组.如果是这种情况,只需对上面的代码片段稍作修改即可.

I believe you have multiple radio groups each with 3 radio buttons. If that is the case, just slight modification in you above code snippet should work for you.

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

请告诉我这是否有帮助.

Please let me know if this helps.

这篇关于从方法获取恒定的假值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆