Java trivia game

jpmasta12666

Baseband Member
Messages
26
Location
Canada
I wanted to know if there is a code that would help me change the question of the game. so for example

If( you get the question correct
the next question comes up

else if( you get the question wrong
game restarts

Thank you
 
Yes, there is some code that will achieve that!

What that code is however, highly depends on what code you have already.
 
I dont have any code for it already cause I actually have no clue what I'd use for it

No code for this bit, or no code for the application overall? If it's no code for the application overall, then take one bit at a time, worry about this bit when you've got a basic framework there. If you have got a basic framework and want to add this in, we'll need to see that code before we can possibly have a chance of helping.
 
No code for that bit. i have the rest of the application. the rest of the application is same code as what you last helped me on

---------- Post added at 03:46 PM ---------- Previous post was at 03:45 PM ----------

heres my whole application so far though idk if you wanna correct anything I have or lend some tips


int roll;
roll = (int)((3 -1 +1)*Math.random() +1);
if(roll==1){
lblQuestion.setText("What is a jump on a skateboard called?");
btnAnswer1.setText("Faceplant");
btnAnswer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
btnAnswer2.setText("Ollie");
btnAnswer2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Correct");
}
});
btnAnswer3.setText(" Jump");
btnAnswer3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
btnAnswer4.setText("A Ninja");
btnAnswer2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});



count = 10;
countdownTimer = new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblCountdown.setText(Integer.toString(count));
count = count -1;
if(count<0){
lblCountdown.setText("Game over");
countdownTimer.stop();
btnAnswer1.setEnabled(false);
btnAnswer2.setEnabled(false);
btnAnswer3.setEnabled(false);
btnAnswer4.setEnabled(false);
}
}

});
countdownTimer.start();
btnAnswer1.setEnabled(true);
btnAnswer2.setEnabled(true);
btnAnswer3.setEnabled(true);
btnAnswer4.setEnabled(true);
}


if(roll == 2)
{
lblQuestion.setText("What is another word for punch?");
btnAnswer1.setText("Hurt");
btnAnswer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
btnAnswer2.setText("Jab");
btnAnswer2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Correct");
}
});
btnAnswer3.setText(" Mangle");
btnAnswer3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
btnAnswer4.setText("Cripple");
btnAnswer4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});

count = 10;
countdownTimer = new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblCountdown.setText(Integer.toString(count));
count = count -1;
if(count<0){
lblCountdown.setText("Game over");
countdownTimer.stop();
btnAnswer1.setEnabled(false);
btnAnswer2.setEnabled(false);
btnAnswer3.setEnabled(false);
btnAnswer4.setEnabled(false);

}
}

});
countdownTimer.start();
btnAnswer1.setEnabled(true);
btnAnswer2.setEnabled(true);
btnAnswer3.setEnabled(true);
btnAnswer4.setEnabled(true);
}
if(roll==3){
lblQuestion.setText("How many strings does a regular guitar have?");
btnAnswer1.setText("4");
btnAnswer1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
btnAnswer2.setText("6");
btnAnswer2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Correct");
}
});
btnAnswer3.setText(" 2");
btnAnswer3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
btnAnswer4.setText("12");
btnAnswer4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblAnswer.setText("Incorrect");
}
});
}

count = 10;
countdownTimer = new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblCountdown.setText(Integer.toString(count));
count = count -1;
if(count<0){
lblCountdown.setText("Game over");
countdownTimer.stop();
btnAnswer1.setEnabled(false);
btnAnswer2.setEnabled(false);
btnAnswer3.setEnabled(false);
btnAnswer4.setEnabled(false);

}
}

});
countdownTimer.start();
btnAnswer1.setEnabled(true);
btnAnswer2.setEnabled(true);
btnAnswer3.setEnabled(true);
btnAnswer4.setEnabled(true);
 
This would work much better if you had abstracted this out into a few different functions with things like "roll" and "count" being global to the java class.

Basically, call a roll function which does your random math then calls another function passing the roll as a parameter and configures things properly. On any button press call some kind of guess function to check. If they are right, call roll again and it'll do everything else for you. If they are wrong set the text to incorrect. Either way, you don't want to add action listeners to an item more than once. it might work, but it's a terrible practice. Create the items once and give it a listener that's generalized enough to do whatever you need it do (aka call apropriate other functions like I'll show below)

To show an example of a button press action listener using what you've provided:

Code:
btnAnswer2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
guess(2);
}
}


public void guess(Int buttonPressed) {
    if (roll == 3 && buttonPressed==2) {
lblAnswer.setText("Correct");
roll();
}
else if (<<Repeat for all correct roll/button combos>>)
.
.
.
else {
lblAnswer.setText("Incorrect");
<<do whatever else you do on a wrong answer>>
}
}
 
Back
Top Bottom