【发布时间】:2016-03-31 07:05:38
【问题描述】:
我的申请是多项选择测验。现在只有两个问题,所以我可以测试逻辑,但是在我弄清楚它之后加起来是 100。真的我有一个新的框架,按钮添加到面板,然后面板添加到 JFrame。然后,我使用 nextQuestion() 方法允许我通过多个问题,只要还有问题。还有一个计时器,我通过他们的计时器,为每个问题提供时间,10 秒。
protected void nextQuestion() {
timer.stop();
currentQuestion++;
if (currentQuestion >= quiz.size()) {
cardLayout.show(QuestionsPanel, "last");
next.setEnabled(false);
//Show however many correct after last question.
//iterate on the collection to count the selected radio buttons
//What im confused about is really how do i tell the program which (options) are right or wrong.
//and if i can do that how do i only count the correct ones.
//or for example do i have to take the total questions and subtract by the ones they didnt choose?
//a code example on how to do this would be great.
} else {
cardLayout.show(QuestionsPanel, Integer.toString(currentQuestion));
startTime = null;
next.setText("Next");
next.setEnabled(true);
timer.start();
}
}
public interface Question {
public String getPrompt();
public String getCorrectAnswer();
public String[] getOptions();
public String getUserResponse();
public void setUserResponse(String response);
public boolean isCorrect();
}
public class ChoiceQuestion implements Question {
private final String prompt;
private final String correctAnswer;
private final String[] options;
private String userResponse;
public ChoiceQuestion(String prompt, String correctAnswer, String... options) {
this.prompt = prompt;
this.correctAnswer = correctAnswer;
this.options = options;
}
@Override
public String getPrompt() {
return prompt;
}
@Override
public String getCorrectAnswer() {
return correctAnswer;
}
@Override
public String[] getOptions() {
return options;
}
@Override
public String getUserResponse() {
return userResponse;
}
@Override
public void setUserResponse(String response) {
userResponse = response;
}
@Override
public boolean isCorrect() {
return getCorrectAnswer().equals(getUserResponse());
}
}
public class QuestionPane extends JPanel {
private Question question;
public QuestionPane(Question question) {
this.question = question;
setLayout(new BorderLayout());
JLabel prompt = new JLabel("<html><b>" + question.getPrompt() + "</b></html>");
prompt.setHorizontalAlignment(JLabel.LEFT);
add(prompt, BorderLayout.NORTH);
JPanel guesses = new JPanel(new GridBagLayout());
guesses.setBorder(new EmptyBorder(10,10,10,10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
List<String> options = new ArrayList<>(Arrays.asList(question.getOptions()));
options.add(question.getCorrectAnswer());
Collections.sort(options);
ButtonGroup bg = new ButtonGroup();
for (String option : options) {
JRadioButton btn = new JRadioButton(option);
bg.add(btn);
guesses.add(btn, gbc);
}
add(guesses);
}
public Question getQuestion() {
return question;
}
public class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
getQuestion().setUserResponse(e.getActionCommand());
}
}
}
}
所以我真的只是在做那个小的 if else 代码块时遇到了问题。如果你们能帮帮我。我有一些关于迭代集合的想法,尽管我不知道该怎么做。这些只是我使用的类和我遇到问题的方法。
【问题讨论】:
-
您可以为
CardLayout使用一个简单的命名约定(即q1、q2等),并简单地保留一个指向当前问题的简单指针。我会更容易确定你是在测验的开始、结束还是中间的某个地方 -
这在
QuizPanel中从same question 中得到了演示,您从...cardLayout.show(panelOfQuestions, Integer.toString(currentQuestion));...中获得了基本代码结构... -
好吧,抱歉。是的,我使用了我只是想知道的基本代码结构,因为过去两个小时我一直在尝试使这个测验起作用,你是怎么做的?它是循环单选按钮并使用 is.selected() 还是使用收集器?
-
@MadProgrammer 保留指向程序的指针会做什么。您不想拥有它,因此它会检查单击了哪些按钮,将其与正确答案进行比较并打印分数。我不确定我在这个逻辑上很糟糕。任何帮助都会很棒。
-
并非如此,您可以使用
currentQuestion值在当前点向Quiz询问Question并检查它的isCorrect值,然后再转到下一个Question。所以在nextQuestion方法中,你可以在currentQuestion++之前检查,但是你需要验证currentQuestion在0和预期的问题数量之间,这样你就可以保持一个连续的计数
标签: java arrays swing collections