好的,所以我做了一个Java程序来测试我的冷战历史日期,我想扩展它,用一个简单的框架覆盖所有科目。我正在考虑制作一个具有不同主题的枚举(它们本身就是枚举)。我知道我可以用JOptionPane来选择一个GUI或其他东西,但我想让它尽可能地动态(就像不必硬编码选项一样)。以下是目前为止的内容:
public class Main {
public List<History> list = new ArrayList<>();
public List<History> generated = new ArrayList<>();
private final Random r = new Random();
private final MainFrame frame = new MainFrame(nextQuestion(), Main.this);
private int correctInFirstGo = History.values().length;
public static void main(String[] args) {
new Main();
}
public Main() {
SwingUtilities.invokeLater(() ->frame.setVisible(true));
}
public void setCorrect(boolean b, int attempts) {
if (list.size() == History.values().length) {
JOptionPane.showMessageDialog(frame, "You got " + correctInFirstGo + "/" + History.values().length);
frame.dispose();
System.out.println(list);
System.out.println(generated);
return;
}
loop:
if (b) {
final History h = nextQuestion();
if (generated.contains(h) && !list.contains(h)) {
System.out.println(h);
frame.setQuestion(h);
} else {
break loop;
}
} else if (attempts != 1) {
correctInFirstGo--;
}
}
private History nextQuestion() {
History[] values = History.values();
History h = values[r.nextInt(values.length)];
if (list.contains(h)) {
return nextQuestion();
}
generated.add(h);
return h;
}
}
public class MainFrame extends JFrame{
private Main m;
private History h;
private JLabel questionLabel;
private JLabel verdictLabel;
private JTextField answerField;
private JButton checkButton;
private JLabel questionNumber;
private JLabel attemptLabel;
private int attempt = 1;
public MainFrame(History h, Main m) {
System.out.println(h);
this.h = h;
this.m = m;
initComponents();
}
private void initComponents() {
questionLabel = new JLabel("", SwingConstants.CENTER);
answerField = new JTextField();
checkButton = new JButton();
verdictLabel = new JLabel("", SwingConstants.CENTER);
questionNumber = new JLabel("");
attemptLabel = new JLabel("" + attempt);
setTitle("History Test");
Container contentPane = getContentPane();
contentPane.setLayout(null);
setQuestion(h);
checkButton.setText("Check");
verdictLabel.setText("");
questionLabel.setBounds(5, 5, 345, 25);
answerField.setBounds(148, 35, 55, 25);
checkButton.setBounds(138, 65, 75, 25);
verdictLabel.setBounds(5, 90, 345, 25);
questionNumber.setBounds(5, 90, 50, 25);
attemptLabel.setBounds(320, 90, 15, 25);
answerField.addActionListener(ae -> checkSolution());
checkButton.addActionListener(ae -> checkSolution());
contentPane.add(questionLabel);
contentPane.add(answerField);
contentPane.add(checkButton);
contentPane.add(verdictLabel);
contentPane.add(questionNumber);
contentPane.add(attemptLabel);
setResizable(false);
setLocationRelativeTo(null);
pack();
setSize(350, 140);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void setQuestion(History h) {
this.h = h;
String question = "What year was the " + h + "?";
questionLabel.setText(question);
answerField.setText("");
m.list.add(h);
questionNumber.setText("" + m.generated.size() + "/" + History.values().length);
}
private void checkSolution() {
answerField.requestFocus();
if (answerField.getText().equals(h.answer())) {
verdictLabel.setText("Correct!");
m.setCorrect(true, attempt);
attemptLabel.setText((attempt = 1) + "");
} else {
verdictLabel.setText("Try again!");
m.setCorrect(false, attempt);
attempt++;
attemptLabel.setText(attempt + "");
}
}
}
public enum History{
// my constants here
private String answer;
private String toString;
private final String[] ignoreCapitals = {"of", "the", "built"};
History(String answer) {
this.answer = answer;
}
public String answer() {
return answer;
}
@Override
public String toString() {
if (toString != null) { return toString; }
final String name = name();
final String[] words = name.replace('$', ''').split("_+");
final StringBuilder result = new StringBuilder(name.length());
for (int i = 0; i < words.length; i++) {
String word = words[i];
final String newWord = performCapitalization(word, i);
if (newWord != null) {
result.append(newWord);
}
if(i < words.length - 1){
result.append(' ');
}
}
return (toString = result.toString());
}
private String performCapitalization(final String oldWord, final int index) {
if(oldWord == null || oldWord.length() == 0){
return null;
}
boolean ignore = false;
if (index != 0) {
for (final String str : ignoreCapitals) {
if(str.equalsIgnoreCase(oldWord)){
ignore = true;
break;
}
}
}
final String newWord = oldWord.toLowerCase();
if(ignore){
return oldWord.toLowerCase();
}
return Character.toUpperCase(newWord.charAt(0)) + newWord.substring(1);
}
}
首先,我会考虑将问题存储在一个文件中(xml/json似乎是自然的选择)。
如果你想在代码中创建问题的好方法,为什么不创建一个具有流畅api的库呢?所以你看到的是这样的内容:
QuestionSet = new QuestionSet("QS1")
.addTopic( new Topic("topic 1")
.addQuestion( new Question("text"...)
.addQuestion( new Question("text"...) )
.addTopic( new Topic("topic 2")
.addQuestion( new Question("text"...) )
您最好使用Hashmap的Hashmap。