我准备了一个灵感来自Trivia Crack的项目。
SporHmap是一个存储3个值(问题)的HashMap。在actionListener方法中,程序从Hashmap获取一个随机密钥,并将问题和答案输出到QuestionClass的JLabel和JButton。
问题在于,我不希望重复这些问题。如果显示了问题,则不应再次显示。我选择了一个值之后使用了 tl.SporHmap.remove(randomValue); 但它没有改变任何东西。
TriviaLinked tl = new TriviaLinked();
tl.SporHmap.put("Basketbolda 3 adımdan fazla atılan adıma ne denir?","Steps");
tl.SporHmap.put("Hindistan'ın ulusal sporu nedir?","Kriket");
tl.SporHmap.put("Süper Lig'de hakeme kırmızı kart gösteren futbolcu kimdir?","Salih Dursun");
Spor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
QuestionClass q = new QuestionClass();
q.getQFrame();
Object[] values = tl.SporHmap.values().toArray();
String randomValue = (String)values[r.nextInt(values.length)];
if(tl.SporHmap.get("Hindistan'ın ulusal sporu nedir?").equals(randomValue)){
q.label.setText("Hindistan'ın ulusal sporu nedir?");
q.answer1.setText("Kriket");
q.answer2.setText("Beyzbol");
q.answer3.setText("Hokey");
q.answer4.setText("Futbol");
tl.SporHmap.remove(randomValue);
q.answer1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "True Answer");
score.setText("Score: "+scr++);
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
}
else if(tl.SporHmap.get("Basketbolda 3 adımdan fazla atılan adıma ne denir?").equals(randomValue)){
q.label.setText("Basketbolda 3 adımdan fazla atılan adıma ne denir?");
q.answer1.setText("Serbest atış");
q.answer2.setText("Dışarı çıkış");
q.answer3.setText("Steps");
q.answer4.setText("Faul");
tl.SporHmap.remove(randomValue);
q.answer3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "True Answer");
score.setText("Score: "+scr++);
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
}
else if(tl.SporHmap.get("Süper Lig'de hakeme kırmızı kart gösteren futbolcu kimdir?").equals(randomValue)){
q.label.setText("Süper Lig'de hakeme kırmızı kart gösteren futbolcu kimdir?");
q.answer1.setText("Erkan Zengin");
q.answer2.setText("Özer Hurmacı");
q.answer3.setText("Salih Dursun");
q.answer4.setText("Aykut Demir");
tl.SporHmap.remove(randomValue);
q.answer3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "True Answer");
score.setText("Score: "+scr++);
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
q.answer4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Wrong Answer");
q.getQFrame().dispose();
Spor.doClick();
}
});
}
}
});
这是我的QuestionClass课程:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class QuestionClass {
public JLabel label,label1;
public JFrame questionFrame;
public JButton answer1,answer2,answer3,answer4;
public QuestionClass() {
questionFrame = new JFrame();
questionFrame.setDefaultCloseOperation(questionFrame.HIDE_ON_CLOSE);
questionFrame.setLayout(new GridLayout(3,2));
questionFrame.setSize(700, 350);
questionFrame.setVisible(true);
questionFrame.setLocationRelativeTo(null);
label = new JLabel("");
questionFrame.add(label);
label1 = new JLabel("");
questionFrame.add(label1);
answer1 = new JButton("");
questionFrame.add(answer1);
answer2 = new JButton("");
questionFrame.add(answer2);
answer3 = new JButton("");
questionFrame.add(answer3);
answer4 = new JButton("");
questionFrame.add(answer4);
}
public JFrame getQFrame() {
return questionFrame;
}
}
答案 0 :(得分:2)
你的方法是正确的:在提出问题之后;从地图中删除它。
换句话说:只需在地图上填写足够的问题即可;随机选择;在提问后,remove()来自地图的相应密钥。
但似乎你正在打电话
tl.SporHmap.remove(randomValue);
仅适用于一个的潜在案例。 不应该是删除的条件。您随机选择一个问题,然后将其删除!
除此之外:你的“对象”模型不太好。拥有那张地图是没有意义的,但是那时候选择了if / elses!相反:你可以创建一个QuizzQuestion类,而QuizzQuestion有:
使用该类,您可以将属于一起的所有值放入这样的QuizzQuestion对象中。
然后你也不需要地图;只需要一个List<QuizzQuestion>
个对象!
鉴于您的评论:基本上,这些可能是您问题的根本原因:
remove
所有可能的情况