Results 1 to 3 of 3
  1. #1
    geork's Avatar
    geork is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    I'm having trouble understanding what you're requiring of the program- specifically. It might behoove you to be more detailed.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    geork's Avatar
    geork is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    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:
    Java 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 :D
    he who laughs last probabely just got the joke

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •