查看正确答案 [英] Check the correct answer

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

问题描述

我开发一个应用程序,是一个问题的比赛。我设法每次回答每个问题,以显示一个不同的按钮,但我无法检查正确的答案。我的做法是:我创建了一个额外的列到数据库,在那里我指明了正确的答案是列(1,2,3或4)。我用这个code为显示在不同的按钮的答案。

I am developing an app that is a question's game. I managed to show the answers to each question to a different button each time but i am having trouble to check the right answer. My approach is: I created an extra column to the database where i indicate the column where the right answer is(1,2,3 or 4). I use this code for showing the answers in different buttons.

cur = dbHelper.getRandomQuestion();
    String corrans = cur.getString(cur.getColumnIndex("CorrectAnswer"));
    a = Integer.parseInt(corrans);
    String question = cur.getString(cur.getColumnIndex("QUESTIONS"));
    String answer0 = cur.getString(cur.getColumnIndex("ANSWER1"));
    String answer1 = cur.getString(cur.getColumnIndex("ANSWER2"));
    String answer2 = cur.getString(cur.getColumnIndex("ANSWER3"));
    String answer3 = cur.getString(cur.getColumnIndex("ANSWER4"));
    txtQuest.setText(question);

    ArrayList<String> lstAnswers = new ArrayList<String>();
    lstAnswers.add(answer0);
    lstAnswers.add(answer1);
    lstAnswers.add(answer2);
    lstAnswers.add(answer3);

     score.setText("Your score is " + b +","+ a);

    Random random = new Random();

    int[] textViews = new int[] { R.id.button1, R.id.button2, R.id.button3, R.id.button4 };
    int textViewIndex = 0;

    while (lstAnswers.size() > 0)   {
    int index = random.nextInt(lstAnswers.size());
    if(a == index){ b = index;}
    else{}
    String randomAnswer = lstAnswers.remove(index);
    ((TextView)findViewById(textViews[textViewIndex])).setText(randomAnswer);
    ++textViewIndex;
                                    }

要每个按钮调用比较a和b的值,然后采取相应的行动。但它似乎没有工作。我明白为什么,但我不能弄明白。任何帮助appeciated。

To each button call to compare the values of a and b and then act accordingly. But it doesnt seem to work. I understand why but i cannot figure it out. Any help appeciated.

推荐答案

使用 Col​​lections.shuffle(名单)洗牌你的答案数组,然后显示答案
   设置一个标记为权利的按钮,其前面回答是正确的,后来比较了tagvalue和DISPLY对错

use Collections.shuffle(list) to shuffle your answer array and then display the answer Set a tag as "right" to your button where anwer is correct and later compare the the tagvalue and disply right or wrong

编辑:下面的事情只是一个大纲

below thing is just an outline

 Here im displaying answers in buttons....

    List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3));
        Collections.shuffle(intList);

       Log.d("ERR","List after shuffling: " + intList);  
       // below answers will be assiagned randomly to buttons...
        btn_cmpTagline[intList.get(0)].setText(answr1);
        btn_cmpTagline[intList.get(0)].setTag("right");
        btn_cmpTagline[intList.get(1)].setText(answr2);
        btn_cmpTagline[intList.get(1)].setTag("wrong");
        btn_cmpTagline[intList.get(2)].setText(answr3);
        btn_cmpTagline[intList.get(2)].setTag("wrong");
        btn_cmpTagline[intList.get(3)].setText(answr4);
        btn_cmpTagline[intList.get(3)].setTag("wrong");

//在点击

   @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_tag1:
            Log.d("ERR", v.getTag().toString());
            if(v.getTag().toString().equalsIgnoreCase("right")){
                //this button has right answer .. do anything 
            }

            break;
        case R.id.btn_tag2:
            Log.d("ERR", v.getTag().toString());
            if(v.getTag().toString().equalsIgnoreCase("right")){
                //this button has right answer .. do anything 
            }

            break;
        case R.id.btn_tag3:
            Log.d("ERR", v.getTag().toString());
            if(v.getTag().toString().equalsIgnoreCase("right")){
                //this button has right answer .. do anything 
            }

                    --
                    --
      }

这篇关于查看正确答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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