Results 1 to 3 of 3

Thread: out of ideas

  1. #1
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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 ||
                ...
    The other rough place is missing curley braces for the code blocks which follow your three if statements.

  3. #3
    v1nsai's Avatar
    v1nsai is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    wow, removing my redundant variable declarations solved everything, you'd think the compiler would have caught that but it made the program just act weird. Thanks a lot for the help!
    C:\DOS > C:\DOS.RUN > RUN.DOS.RUN

Similar Threads

  1. Replies: 8
    Last Post: 11-05-2010, 11:26 PM
  2. Research Ideas
    By hawaiifiver in forum Forum Lobby
    Replies: 2
    Last Post: 02-03-2009, 05:43 AM
  3. Any Ideas for a Project?
    By quddusaliquddus in forum Advanced Java
    Replies: 19
    Last Post: 12-19-2008, 05:22 PM
  4. Java Performance Ideas
    By developer321 in forum Advanced Java
    Replies: 4
    Last Post: 06-28-2008, 05:16 PM
  5. Merging Ideas
    By CompleteBeginner in forum New To Java
    Replies: 1
    Last Post: 05-19-2008, 03:15 PM

Posting Permissions

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