Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2008, 09:41 PM
geork's Avatar
Member
 
Join Date: Jan 2008
Posts: 14
geork is on a distinguished road
AnimalGame dilema
hey guys!
i have got a problem which i cannot solve.
i made this program where the user has to guess an animal that the computer thought of (randomly). but when i execute it it does not go right. here's the program:
Code:
import javax.swing.*; import java.util.Scanner ; import java.util.Random; import java.awt.FlowLayout; import javax.swing.event.DocumentListener; import javax.swing.event.DocumentEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.*; class AnimalGame extends JFrame implements ItemListener, ActionListener{ public static void main(String args[]){ new AnimalGame(); } JLabel q1 = new JLabel("is it big?(sheep to elephant size)"); JLabel q2 = new JLabel(); JLabel q3 = new JLabel("is it a mammel?"); JLabel q4 = new JLabel(); JLabel q5 = new JLabel("does it eat meat?"); JLabel q6 = new JLabel(); JLabel q7 = new JLabel("does it live underground?"); JLabel q8 = new JLabel(); JLabel q9 = new JLabel("does it have hooves?"); JLabel q10 = new JLabel(); JLabel q11 = new JLabel(); JLabel q12 = new JLabel(); JComboBox cc = new JComboBox(); JButton bb = new JButton("go"); static void Sleep1001(){ try{ Thread.sleep(1000) ; } catch (InterruptedException e){ System.out.println("program ended") ; } } public AnimalGame(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; setLayout(new FlowLayout()) ; setTitle("animal game"); add(q1); q1.setVisible(false); add(q2); q2.setVisible(false); // all the other buttons are set invisible add(cc); cc.addItem("fox"); cc.addItem("rabbit"); cc.addItem("mouse");// another 12 animals listed add(bb); bb.setEnabled(true); bb.addActionListener(this); setSize(300, 500); setVisible(true); } public void actionPerformed(ActionEvent e){ bb.setEnabled(false); cc.setVisible(true); String p; String y = "yes"; String n = "no"; int op = new Random().nextInt(15)+1; int po = op; if (po == 1){ q2.setText(n); q4.setText(y); q6.setText(y); q8.setText(y); q10.setText(n); p = "fox"; } if (po == 2){ q2.setText(n); q4.setText(y); q6.setText(n); q8.setText(y); q10.setText(n); p = "rabbit"; // goes on for the next 13 animals q1.setVisible(true); Sleep1001(); q2.setVisible(true); Sleep1001(); q3.setVisible(true); Sleep1001(); // sets all JLabels visible cc.setEnabled(true); q11.setText(p); } public void itemStateChanged(ItemEvent e){ cc.setEnabled(false); if( cc.getSelectedItem()== q11){ q12.setText("you win!!!"); }else{ q12.setText("you lose"); } } }
it always sets the same answeres and doesn't deam any answer right.
thankyou
__________________
he who laughs last probabely just got the joke
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-16-2008, 03:10 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
I'm having trouble understanding what you're requiring of the program- specifically. It might behoove you to be more detailed.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-17-2008, 10:26 AM
geork's Avatar
Member
 
Join Date: Jan 2008
Posts: 14
geork is on a distinguished road
i want the program to choose randomely choose an animal(i didn't list them all as there are 15) then answere the 5 questions given (i.e. the questions on labels 1, 3 ,5 ,7 and 9) and answere them depending on the animal (in the if statements in the actionPerformed method). then string "p" gets given the value of the animal (again, in the if statements). then, the labels (both questions and answeres) get shown and the user has to select an animal(using the combo box)
if the animal is the same as the user selected then they win, else they lose.
i made one modification:
Code:
q11.setText(p); } public void itemStateChanged(ItemEvent e){ cc.setEnabled(false); if( cc.getSelectedItem()== q11.getText()){
but it always seems to chose the same number (thus, the same animal) and it complains that "variable p may not have been intialized".
hope that helps you help me
__________________
he who laughs last probabely just got the joke
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

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



All times are GMT +3. The time now is 07:15 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org