Fill the blank so that weight is tested in two ways:
weight
must be equal or greater than 136weight
must be less than or equal to 147
// check that the weight is within range
if ( weight >= 136 && weight <= 147 )
System.out.println("In range!" );
else
System.out.println("Out of range." );
Another correct answer is:
// check that the weight is within range
if ( 136 <= weight && weight <= 147 )
System.out.println("In range!" );
else
System.out.println("Out of range." );
Here is another correct (but unclear) answer:
// check that the weight is within range
if ( weight < 148 && 135 < weight )
System.out.println("In range!" );
else
System.out.println("Out of range." );
There are more than a dozen correct answers. But the best answers are both correct and clear.
The boxer must weigh enough (weight >= 136),
and must also not weigh too
much (weight <= 147).
The results of the two tests are combined with the and-operator, &&
.
A common mistake is to use the wrong operands with the relational operators. The following does not work:
if ( 136 <= weight <= 147 ) // wrong
The above is incorrect because the first <=
will produce a boolean, and now the second <=
would try to compare a boolean to an integer.
Here is a JavaScript version of the program:
Try the program with the weight 140.