1

我不知道这是我遇到的愚蠢问题还是什么,但对于我的生活,我无法找出解决方案。我正在编写一个包含 20 个问题的测验程序。该程序询问每个问题,用户有 5 个选项,即 5 个 JRadio 按钮,一旦用户选择一个答案,他可以点击下一个以转到下一个问题或上一个以查看问题。我遇到的问题是,一旦用户回答问题并点击下一个选择的上一个单选按钮保持选中状态,我的意思是如果问题 1 的答案 A 和点击下一个选项 A 将被选择用于问题 2 等等。5个单选按钮在一个按钮组中,我使用清除选择方法清除选择它工作正常,除非用户点击上一个按钮来查看问题,一旦他点击下一个按钮继续所有选择变得清晰,

我将在下面添加下一个和以前的实现。任何想法将不胜感激。

// 实现下一个按钮

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

    }); 

// 实现上一个按钮

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

1 回答 1

2

如您所述,当用户点击下一步按钮时,单选按钮被清除,如果您不保存用户的选择,用户当然无法检查他以前的答案。

所以我认为你需要创建一个HashMap,它使用questionIndex作为键和answer值来存储用户的选择。每次用户点击下一个按钮时,只需将用户的选择放入带有问题 ID 的 hashmap 中。当用户点击上一个按钮时,只需获取上一个问题的索引并从哈希图中获取答案,并选择相应的单选按钮。

于 2012-12-07T19:03:37.410 回答