This project is due in a matter of hours so please help, its very very urgent...
So, i added my buttons to a panel to make them visible. I have a Next and a Finish button, the Next button is meant to shuffle the "deck" (card layout), of questions and spit out one that hasn't been used yet that corresponds to an array of answers that you then answer using the provided bubbles. Here are the bugs that are happening...
1) Next button doesn't appear even though its coded into the visible panel 2) Next button appears randomly and when it does, when clicked, just tells me that i got all the answers wrong 3) When i click Finish or Next after answering the question it doesn't detect which answer i selected correctly or does, but says its wrong anyway 4) Finish button doesn't run ShowResult
First the main code that you run, Quiz.java
public class Quiz extends JFrame{
JPanel p=new JPanel();
CardLayout cards=new CardLayout();
int numQs;
int wrongs=0;
int total=0;
//place answers here
String[][] answers={
{"Hitler","Stalin","Mussolini","Alfred Hitchcock"},
{"True","False"},
{"1940","1951","1939","1941"},
{"Lightning War", "Lightning Attack", "Thunder Attack", "Quick Attack"},
};
//place questions here
RadioQuestion questions[]={
new RadioQuestion("Who was the leader of Nazi Germany during WWII", answers[0],1,this),
new RadioQuestion("The invasion of poland was sucessful", answers[1],2,this),
new RadioQuestion("What year did the attack on Pearl Harbor occur?", answers[2], 4,this),
new RadioQuestion("What does Blitzkreig mean?", answers[3], 2,this),
};
public static void main(String args[]){
new Quiz();
}
public Quiz(){
super("Quiz Game");
setResizable(true);
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.setLayout(cards);
numQs=questions.length;
for(int i=0;i<numQs;i++){
p.add(questions[i],"q"+i);
}
Random r=new Random();
int i=r.nextInt(numQs);
cards.show(p, "q"+i);
add(p);
setVisible(true);
}
public void next() {
if((total-wrongs)==numQs){
showSummary();
}else{
Random r=new Random();
boolean found=false;
int i=0;
while(!found){
i=r.nextInt(numQs);
if(!questions[i].used){
found=true;
}
}
cards.show(p, "q"+i);
}
}
private void showSummary() {
JOptionPane.showMessageDialog(null, "All Done!, here are your results"+"\nNumber of incorrect Answers: \t"+wrongs+
"\nNumber of correct answers: \t"+(total-wrongs)+
"\nAverage Incorrect Answers per Question: \t"+((float)wrongs/numQs)+
"\nPercent Correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+"%");
System.exit(0);
}
}
Now for the RadioListener...
package GameStructure;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.BoxLayout;
public class RadioQuestion extends JPanel implements ActionListener{
int correctAns;
int selected;
//questions
JPanel qPanel=new JPanel();
//answers
JPanel aPanel=new JPanel();
JRadioButton[] responses;
ButtonGroup group=new ButtonGroup();
//bottom
JPanel botPanel=new JPanel();
static JButton next=new JButton("Next");
JButton finish=new JButton("Finish");
private Quiz quiz;
public boolean used;
public static void main (String args[]){
JFrame frame=new JFrame("WWII Quiz Review Game");
frame.setSize(400,300);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
}
public RadioQuestion(String q, String[] options, int ans, Quiz quiz){
this.quiz=quiz;
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
correctAns=ans;
//question
qPanel.add(new JLabel(q));
add(qPanel);
//answer
responses=new JRadioButton[options.length];
for(int i=0;i<options.length;i++){
responses[i]=new JRadioButton(options[i]);
responses[i].addActionListener(this);
group.add(responses[i]);
aPanel.add(responses[i]);
}
add(aPanel);
//bottom
next.addActionListener(this);
finish.addActionListener(this);
botPanel.add(finish);
add(botPanel);
botPanel.add(next);
}
public RadioQuestion(String string, String[] answers, int i) {
// TODO Auto-generated constructor stub
}
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
//next button
if(src.equals(next)){
showResult();
if(selected==correctAns){
used=true;
quiz.next();
}
}
//finish button
if(src.equals(finish)){
showResult();
}
}
private void showResult() {
String text=responses[selected].getText();
quiz.total++;
if(selected==correctAns){
JOptionPane.showMessageDialog(null, text+ " is the correct answer\nWell done!");
}else{
quiz.wrongs++;
JOptionPane.showMessageDialog(null, text+" is wrong\n Sorry");
}
}
}
Any advice would be fantastic. Its due tomorrow morning.
Aucun commentaire:
Enregistrer un commentaire