Results 1 to 5 of 5
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Exclamation guessing game help

    yyyyyyyyyy
    Last edited by yasmin k; 11-08-2009 at 12:24 PM.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    As written, the declarations of min and max are inside the actionPerformed() method.
    Their value will be initialized as the declarations are evaluated every time actionPerformed() is called.

    They will only be initialized once if the declarations are moved outside, but still within class Action:
    Java Code:
    static class Action implements ActionListener {
    
    [COLOR="Navy"]    int max, min, guess;
        boolean guessed;
        guessed = false;
        max = 1000;
        min = 0;
        guess = (0 + 1000) / 2;[/COLOR]
    
        public void actionPerformed(ActionEvent e) {
            ...

  3. #3
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Unhappy

    thank you for your suggestion but
    i get the three errors in the follwoing areas highlighted in red,

    i tried fixing it by adding open curly braces after "boolean guessed; {" and close curly bracket after " guess = (0 + 1000) / 2;}" which removes all the errors but the functionality does not work properly.

    any suggestion again :D



    buttonCorrect.addActionListener(a);
    panel2.add(label);

    }
    });

    }

    static class Action implements ActionListener {

    int max, min, guess;
    boolean guessed;
    guessed = false;
    max = 1000;
    min = 0;
    guess = (0 + 1000) / 2;

    public void actionPerformed(ActionEvent e) {


    if (e.getSource() == buttonHigh) {
    min = guess;

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default use {...} or initializers

    Perhaps I shouldn't try to write these things in the middle of the night. :(

    Of course it won't compile; I should not have put assignment statements among the class declarations.
    Your fix was on the right track, but did not go far enough;
    all four assigments would have to be inside the {...}
    Another approach is initializers; they are allowed even though they look almost like assignments:
    Java Code:
    static class Action implements ActionListener {
        int max = 1000, min = 0;     [COLOR=Green]// initializers are legal[/COLOR]
        int guess = (min+max) / 2;
        boolean guessed = false;
    
        // max = 1000; min = 0;    [COLOR=Red]// assignments are illegal[/COLOR]
    
        public void actionPerformed(ActionEvent e) {
            ...

  5. #5
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

    Thank you soo much for your time and suggestions, you have helped me greatly. ;)

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 01:13 PM
  3. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 01:33 AM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  5. Tic Tac Game
    By loggen in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 08:36 AM

Posting Permissions

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