If Statements


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: If Statements

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    5

    Question If Statements

    Hi all

    Is this a legit if statement. It is a button that changes a ball colour from blue to red on a click. And also display the colour in a text field. I have experimented several methods but can't get it to work. test is an int set to 0.

    void jButton1_mouseClicked(MouseEvent e) {

    if (test < 1)
    (aBall.color = (color.red)); test--; jTextField1.setText("Blue");
    else
    (aBall.color = (color.blue)); test++; jTextField1.setText("Red");
    repaint();
    }

    Cheers

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    No

    You enclose statements in parentheses and have no valid use of brackets
    in your if statement.
    Code:
    if (test < 1) {
      aBall.color = color.red; 
      test--; 
      jTextField1.setText("Blue");
    } else {
      aBall.color = color.blue; 
      test++; 
      jTextField1.setText("Red");
    }
    repaint();
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    5
    Hi sjalle

    I get an error about the test variable now. I declared it like this

    public static final int test = 0;

    Any advice

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    public access can only be specified for variables defined at class level
    so you cannot define a public variable inside a method.
    This goes for static variables too.
    If you are not using the variable inside static methods and you have no
    need for accessing the variable via the class name like:

    int a=AnyClass.aVariable;

    the don't use the static either.

    final 'variables' are basically the same as the c #define values,
    they are final, in other words the value they receive in the define statement:

    public final int x=10;

    is the only value they can ever get.

    So i guess your code will compile if you leave out final. As a rule you should
    only define variables as final and/or static if you need the functioonality I
    have mentioned above.

    Happy reading....
    eschew obfuscation

  5. #5
    Join Date
    Mar 2005
    Posts
    5

    Talking Next error

    Got that working, thanks.
    Last edited by scooby316; 03-08-2005 at 09:35 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center