简体   繁体   English

Android quiz 如何根据答案显示问题?

[英]Android quiz How to show the question based on the answer?

I am currently doing a quiz app and the question should be coming out based on the answer.我目前正在做一个测验应用程序,问题应该根据答案出来。 The below is my code.以下是我的代码。 I have been looking online to try various options and cannot find a way to resolve it without making lots of errors.我一直在网上寻找各种选择,但找不到一种方法来解决它而不会犯很多错误。 The code for my quiz activity is below.我的测验活动的代码如下。 Thanks.谢谢。

quiz.java测验.java

public class quiz extends AppCompatActivity implements View.OnClickListener {

    TextView questionNo, question;
    RadioGroup rg;
    Button quiz_nextBtn, quiz_PrevBtn;
    RadioButton rb1, rb2;
    int current_question = 0;
    int next_question=0;

    private quizAdapter[] questionBank = new quizAdapter[]{
            new quizAdapter(R.string.q1, R.string.q1_a1, R.string.q1_a2),
            new quizAdapter(R.string.q2, R.string.q2_a1, R.string.q2_a2),
            new quizAdapter(R.string.q3, R.string.q3_a1, R.string.q3_a2),
            new quizAdapter(R.string.q4, R.string.q4_a1, R.string.q4_a2),
            new quizAdapter(R.string.q5, R.string.q5_a1, R.string.q5_a2),
            new quizAdapter(R.string.q6, R.string.q6_a1, R.string.q6_a2),
            new quizAdapter(R.string.q7, R.string.q7_a1, R.string.q7_a2)
    };

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

        rg = findViewById(R.id.quiz_answer);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        questionNo = findViewById(R.id.quiz_questionNo);
        question = findViewById(R.id.quiz_question);
        quiz_nextBtn = findViewById(R.id.quiz_nextBtn);
        quiz_PrevBtn = findViewById(R.id.quiz_PrevBtn);

        quiz_nextBtn.setOnClickListener(this);
        quiz_PrevBtn.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.quiz_nextBtn:
                if(current_question<7){
                    current_question++;
                    if(current_question == 6){
                        question.setText("We are finish");
                    }
                    else {
                        if(current_question == 0 && rb1.isChecked()){
                            current_question = 1;
                        }
                        else if (current_question == 0 && rb2.isChecked()){
                            current_question = 4;
                        }
                        else if (current_question == 1 && rb1.isChecked()){
                            current_question = 2;
                        }
                        else if (current_question == 1 && rb2.isChecked()){
                            current_question = 3;
                        }
                        else if (current_question == 4 && rb1.isChecked()){
                            current_question = 5;
                        }
                        else if (current_question == 4 && rb2.isChecked()){
                            current_question = 6;
                        }

                        updateQuestion();
                    }

                }
                break;
            case R.id.quiz_PrevBtn:
                if (current_question > 0) {
                    current_question = (current_question - 1) % questionBank.length;
                    updateQuestion();
                }
        }
    }

    private void updateQuestion() {
        Log.d("Current", "onClick: " + current_question);

        question.setText(questionBank[current_question].getQuestion());
        rb1.setText(questionBank[current_question].getRb1());
        rb2.setText(questionBank[current_question].getRb2());
    }
}

quizAdapter.java quizAdapter.java

public class quizAdapter {

    private int question;
    private int rb1;
    private int rb2;

    public quizAdapter(int question, int rb1, int rb2)
    {
        this.question = question;
        this.rb1 = rb1;
        this.rb2 = rb2;
    }

    public int getQuestion()
    {
        return question;
    }

    public int getRb1()
    {
        return rb1;
    }

    public int getRb2()
    {
        return rb2;
    }

}

I think a better way would be to use a HashMap and set the quiz answers and questions as key value pairs , for what your trying to implement you will have multiple keys ( the value of the current question and option chosen ) for a single value( the next question to be shown ).我认为更好的方法是使用 HashMap 并将测验答案和问题设置为键值对,对于您尝试实现的内容,您将有多个键(当前问题的值和选择的选项)用于单个值(下一个要显示的问题)。 one way to do it would be to create a custom HashMap like below一种方法是创建一个自定义 HashMap 如下所示

 import java.util.HashMap;
import java.util.Map;
 
class Key<K1, K2> {
    public K1 key1;
    public K2 key2;
 
    public Key(K1 key1, K2 key2) {
        this.key1 = key1;
        this.key2 = key2;
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
 
        Key key = (Key) o;
 
        if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) return false;
        if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) return false;
 
        return true;
    }
 
    @Override
    public int hashCode() {
        int result = key1 != null ? key1.hashCode() : 0;
        result = 31 * result + (key2 != null ? key2.hashCode() : 0);
        return result;
    }
 
    @Override
    public String toString() {
        return "[" + key1 + ", " + key2 + "]";
    }
}
 
class Main
{
    public static void main(String[] args) {
        //  Create a HashMap with Key as key
        Map<Key, String> multiKeyMap = new HashMap<>();
 
        // [key1, key2] -> value1
        Key k12 = new Key("key1", "key2");
        multiKeyMap.put(k12, "value1");
 
        // [key3, key4] -> value2
        Key k34 = new Key("key3", "key4");
        multiKeyMap.put(k34, "value2");
 
        // print multi-key map
        System.out.println(multiKeyMap);
 
        // print value corresponding to key1 and key2
        System.out.println(multiKeyMap.get(k12));
    }
}

more explanation can be found in this link https://www.techiedelight.com/implement-map-with-multiple-keys-multikeymap-java/更多解释可以在这个链接中找到https://www.techiedelight.com/implement-map-with-multiple-keys-multikeymap-java/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM