andy
andy

Reputation: 391

compare an int to 2 values

I am trying to change the background colour of a JTextField based on the value of an INT. Why does the following comparison code not work?

if (braincomplete < 0 && > 10){
        //set the colour red        
    }
if (braincomplete > 10 &&  <18){
        //set the colour yellow 
}
if (braincomplete >18){
    //set the colour green
}

I thought && was correct for Java?

Upvotes: 2

Views: 91

Answers (2)

reaz
reaz

Reputation: 735

You should rewrite the variable

if (braincomplete > 0 && braincomplete  <= 10){
        //set the colour red

    }
    if (braincomplete > 10 && braincomplete  <=18){
        //set the colour yellow

    }
    if (braincomplete >  18){
        //set the colour green

Upvotes: 2

Achintya Jha
Achintya Jha

Reputation: 12843

Your if block should be like this if you want to compare two values inside if block

if (braincomplete < 0 && braincomplete > 10){
    //set the colour red

}
if (braincomplete > 10 && braincomplete <18){
    //set the colour yellow

}
if (braincomplete >18){
    //set the colour green
}

I dont think you want to check this condition

if (braincomplete < 0 && braincomplete > 10)

You may want to check value of braincomplete should be between 0 to 10 . So it should be:

if (braincomplete > 0 && braincomplete < 10)

Upvotes: 4

Related Questions