Results 1 to 3 of 3
Thread: AnimalGame dilema
- 02-13-2008, 08:41 PM #1
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:
it always sets the same answeres and doesn't deam any answer right.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"); } } }
thankyouhe who laughs last probabely just got the joke
- 02-16-2008, 02:10 AM #2
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)
- 02-17-2008, 09:26 AM #3
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:
but it always seems to chose the same number (thus, the same animal) and it complains that "variable p may not have been intialized".Java Code:q11.setText(p); } public void itemStateChanged(ItemEvent e){ cc.setEnabled(false); if( cc.getSelectedItem()== q11.getText()){
hope that helps you help me :Dhe who laughs last probabely just got the joke


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks