jayNinoMan
jayNinoMan

Reputation: 13

Boolean expression in Java

I'm starting to learn about boolean expressions. I am trying to figure out the following question:

Suppose age1, age2, and age3 are int variables, and suppose answer is a boolean variable. Write an expression that assigns answer the value true exactly when age1 is less than or equal to age2 AND age2 is less than or equal to age3. Otherwise answer should be assigned false.

I have tried a few things but am relatively new to Java. I am able to make the answer print True but something is still wrong with my numbers.

This is wrong:

age1=7;
age2=10;
age3=12;
boolean a= (age1<=age2);
boolean b= (age2<=age3);
boolean answer= (a&&b);

I'm just not sure how to fix this or what exactly is going on in the code; what am I doing wrong?

Upvotes: 1

Views: 1157

Answers (1)

m0bi5
m0bi5

Reputation: 9475

The code your given should work perfectly.

age1=7;
age2=10;
age3=12;
boolean a= (age1<=age2);
boolean b= (age2<=age3);
boolean answer= (a&&b);

But as the questions specifies an "expression" try this out:

boolean answer=age1<=age2 && age2<=age3;

Upvotes: 3

Related Questions