Results 1 to 3 of 3
Thread: out of ideas
- 03-26-2009, 08:21 PM #1
out of ideas
Got a project due tonight, professor hasn't been responding. I can probably get an extension since I wrote her something like 3 days ago and haven't gotten a reply, but I'm really trying to get past this and keep working, I like my code to be nice and polished when I turn it in.
This is just an app that plays "guess the number". It chooses a number between 1 and 1000, then accepts input from the user. When the user enters a number, the background turns red to indicate "warmer" and with each subsequent input it compares the new input to the previous one and turns red or blue to indicate "hot or cold".
I'm having a few problems, first of all it only seems to respond to the first input. After it turns red it stays red no matter what is input, and doesn't seem to accept any other input. After every input the JTextField is supposed to clear and the JLabel is supposed to change its text to "warmer" or "colder", but neither of those are happening, it just kind of sticks on red and stays there.
Anyway, I'd appreciate any advice on this.
Java Code:import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JLabel; import java.util.Random; import javax.swing.SwingConstants; import java.awt.Color; public class numGuess extends JFrame { //the variables private int lastGuess; private int guess; private int random; private JLabel challenge; private JTextField input; public numGuess() { //menu bar title super( "Guess the number, human." ); setLayout( new FlowLayout() ); //setting the random number Random rndm = new Random(); int random = 1 + rndm.nextInt( 1000 ); //display the challenge JLabel challenge = new JLabel( "I have a number between 1 and 1000.\n"+ "Can you guess my number?", SwingConstants.LEFT ); add( challenge ); //accepts input JTextField input = new JTextField( 5 ); input.setText( "???" ); add( input ); //add a listener for the input box and declare action to be taken textHandler handler = new textHandler(); input.addActionListener( handler ); } private class textHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { if( event.getSource() == input ) lastGuess = guess;//save input from last guess guess = Integer.parseInt( event.getActionCommand() );//get input for new guess //compare the guesses, if this is the first guess then any answer is closer than nothing. //if new guess is closer than last guess if( lastGuess == 0 || Math.abs( random - guess ) < Math.abs( random - lastGuess ) ) getContentPane().setBackground( Color.RED ); input.setText( "" ); //if new guess is further than last guess if( Math.abs( random - guess ) > Math.abs( random - lastGuess ) ) getContentPane().setBackground( Color.BLUE ); input.setText( "" ); //if guess is correct if( guess == random ) challenge.setText( "Correct!" ); input.setText( "!!!" ); getContentPane().setBackground( Color.GREEN ); } } }Java Code:import javax.swing.JFrame; public class numWin { public static void main( String args[] ) { numGuess guesser = new numGuess(); guesser.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); guesser.setSize( 500, 150 ); guesser.setVisible( true ); } }C:\DOS > C:\DOS.RUN > RUN.DOS.RUN
- 03-26-2009, 10:17 PM #2
The other rough place is missing curley braces for the code blocks which follow your three if statements.Java Code:// Member variable declarations in class scope. private int random; // jvm initializes this to zero. private JLabel challenge; // null private JTextField input; // null public numGuess() { ... // Local variable declaration hides/shadows // member variable of same name. int random = 1 + rndm.nextInt( 1000 ); // To fix this remove the [i]int[/i] type declaration: // random = 1 + rndm.nextInt( 1000 ); // Now this line assigns a value to the member variable. // Shadows member variable 'challenge'. JLabel challenge = new JLabel( "I have a number between 1 and 1000.\n"+ "Can you guess my number?", SwingConstants.LEFT ); ... // Declaration as a local variable hides 'input' // which is declared in class scope. JTextField input = new JTextField( 5 ); ... private class textHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { ... //if new guess is closer than last guess // To verify this idea about local variables shadowing // member variables try accessing them before you want // to use them to see what happens: System.out.println("random = " + random); System.out.println("challenge = " + challenge); System.out.println("input = " + input); if( lastGuess == 0 || ...
- 03-27-2009, 01:35 AM #3
Similar Threads
-
I got a problem with StreamCorruptedException, any ideas?
By Goodwine in forum New To JavaReplies: 8Last Post: 11-05-2010, 10:26 PM -
Research Ideas
By hawaiifiver in forum Forum LobbyReplies: 2Last Post: 02-03-2009, 04:43 AM -
Any Ideas for a Project?
By quddusaliquddus in forum Advanced JavaReplies: 19Last Post: 12-19-2008, 04:22 PM -
Java Performance Ideas
By developer321 in forum Advanced JavaReplies: 4Last Post: 06-28-2008, 04:16 PM -
Merging Ideas
By CompleteBeginner in forum New To JavaReplies: 1Last Post: 05-19-2008, 02:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks