Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-04-2013, 09:46 AM   #1
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Java trivia game

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
__________________

jpmasta12666 is offline   Reply With Quote
Old 06-06-2013, 09:43 AM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java trivia game

Yes, there is some code that will achieve that!

What that code is however, highly depends on what code you have already.
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 06-06-2013, 12:36 PM   #3
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java trivia game

I dont have any code for it already cause I actually have no clue what I'd use for it
jpmasta12666 is offline   Reply With Quote
Old 06-06-2013, 05:26 PM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java trivia game

Quote:
Originally Posted by jpmasta12666 View Post
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.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 06-07-2013, 03:46 PM   #5
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java trivia game

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);
jpmasta12666 is offline   Reply With Quote
Old 06-09-2013, 09:14 PM   #6
Baseband Member
 
Join Date: Jan 2013
Location: Canada
Posts: 26
Default Re: Java trivia game

So idk what the code would be
jpmasta12666 is offline   Reply With Quote
Old 06-11-2013, 10:31 PM   #7
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Java trivia game

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>>
}
}
__________________

__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 04:36 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0