質問

I dont know if this is a silly issue that i am having or what but for the life of me i cant figure out the solution. I am writing a quiz program with 20 questions. The program asks each question and the user has 5 choices which are 5 JRadio Buttons once the user choose one answer he can hit next to go to the next question or previous to review a question. The issue that I am having is once the user answers a question and hit next the previous radio button selected stays selected, what i mean is if a answer A for question 1 and hit next choice A will be selected for question 2 and so on. the 5 radio buttons are in a button group and i used the clear selection method to clear the selection it works fine except if the user hits the previous button to review a question once he hits the next button to continue all the selections get clear, lets say the user answers 10 questions and goes back to review question number 3 once he hits next to go back up to question 10 all the questions in between will get cleared.

I will add the next and previous implementations below. Any ideas will be much appreciated.

// implements next button

  nextBT.setPreferredSize(new Dimension(70, 30));
    nextBT.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {

            int nextQuestion = -1;
            boolean answer = getAnswer(currentQuestion, selectedButton()).equals(getCorrectAnswer(currentQuestion));
            questionHistory[currentHistoryIndex][0] = currentQuestion;
            questionHistory[currentHistoryIndex][1] = selectedButton();

            if (currentHistoryIndex == maxHistoryIndex) {

                //generate next question number to use
                int currentLevel = currentQuestion / 25;
                int nextLevel = currentLevel;
                if (answer) {
                    if (currentLevel < 3) {
                        nextLevel++;
                    }
                } else {
                    if (currentLevel > 0) {
                        nextLevel--;
                    }
                }
                while (true) {
                    int k = 0;
                    Random randomNum = new Random();
                    nextQuestion = nextLevel * 25 + (int) (randomNum.nextInt(25));
                    for (k = 0; k < maxHistoryIndex; k++) {
                        if (questionHistory[k][0] == nextQuestion) {
                            break;
                        }
                    }
                    if (k == maxHistoryIndex) {
                        break;
                    }
                }
                currentHistoryIndex++;
                maxHistoryIndex++;
                if (maxHistoryIndex == 19) {
                    nextBT.setEnabled(false);

                } else {
                    nextBT.setEnabled(true);
                }

            } else {
                // returning to question already on list  
                currentHistoryIndex++;

                nextQuestion = questionHistory[currentHistoryIndex][0];
                int nextAnswer = questionHistory[currentHistoryIndex][1];
                setSelectedButton(nextAnswer);
            }

            if (currentHistoryIndex == 19) {
                nextBT.setEnabled(false);
            }

            currentQuestion = nextQuestion;
            questionHistory[currentHistoryIndex][0] = currentQuestion;
            questionHistory[currentHistoryIndex][1] = selectedButton();

            question.setText(questions[currentQuestion * 7]);
            rb1.setText(questions[(currentQuestion * 7) + 1]);
            rb2.setText(questions[(currentQuestion * 7) + 2]);
            rb3.setText(questions[(currentQuestion * 7) + 3]);
            rb4.setText(questions[(currentQuestion * 7) + 4]);
            rb5.setText(questions[(currentQuestion * 7) + 5]);

            previousBT.setEnabled(true);

            //setSelectedButton(questionHistory[currentHistoryIndex][1]);
            questionCountLB.setText("Question " + (currentHistoryIndex + 1) + " of 20");


            //if(bg.isSelected()){



            bg.clearSelection();
        }

    }); 

// implements previous button

    previousBT.setPreferredSize(new Dimension(120, 30));
    previousBT.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {

            nextBT.setEnabled(true);
            questionHistory[currentHistoryIndex][1] = selectedButton();

            currentHistoryIndex--;
            if (currentHistoryIndex == 0) {
                previousBT.setEnabled(false);
            }
            if (currentHistoryIndex > 0) {
                previousBT.setEnabled(true);
            }
            int nextQuestion = questionHistory[currentHistoryIndex][0];
            currentQuestion = nextQuestion;
            question.setText(questions[currentQuestion * 7]);
            rb1.setText(questions[(currentQuestion * 7) + 1]);
            rb2.setText(questions[(currentQuestion * 7) + 2]);
            rb3.setText(questions[(currentQuestion * 7) + 3]);
            rb4.setText(questions[(currentQuestion * 7) + 4]);
            rb5.setText(questions[(currentQuestion * 7) + 5]);

            setSelectedButton(questionHistory[currentHistoryIndex][1]);
            questionCountLB.setText("Question " + (currentHistoryIndex + 1) + " of 20");
        }
    });
役に立ちましたか?

解決

As you described, when user hit next button, the radio buttons are cleared, if you don't save the user's selection, of course the user cannot check his previous answers.

So I think you need to create a HashMap, which uses the questionIndex as the key, and answer as the value, to store the user's selections. Each time user hits next button, just put user's selection into hashmap with question's ID. When user hits previous button, just get the previous question's index and get the answer from the hashmap, and make the corresponding radio button selected.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top