我不知道这是一个愚蠢的问题,我有什么或者除了我的生活,我无法弄清楚解决方案。我正在写一个有20个问题的测验计划。该程序询问每个问题,并且用户有5个选项,即5个JRadio按钮,一旦用户选择一个他可以接下来回答的答案,转到下一个问题或之前的问题。我遇到的问题是,一旦用户回答问题并点击下一个选定的选择保持选择的下一个单选按钮,我的意思是如果问题1的答案A和下一个选择A将被选为问题2,依此类推。 5个单选按钮位于按钮组中,我使用清除选择方法清除选择它正常工作,除非用户点击上一个按钮查看问题,一旦他点击下一个按钮继续所有选择清除,让假设用户回答了10个问题并回过头来回顾第3个问题,一旦他再次回到问题10,其间的所有问题都将被清除。
我将在下面添加下一个和之前的实现。 任何想法将不胜感激。
//实现下一个按钮
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");
}
});
答案 0 :(得分:2)
正如您所描述的,当用户点击下一个按钮时,单选按钮被清除,如果您不保存用户的选择,当然用户无法检查他之前的答案。
所以我认为你需要创建一个HashMap
,它使用questionIndex
作为键,answer
作为值,来存储用户的选择。每次用户点击下一个按钮时,只需将用户的选择放入带有问题ID的哈希图中。当用户点击上一个按钮时,只需获取上一个问题的索引并从散列图中获取答案,然后选择相应的单选按钮。