Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is between two numbers with Java

I have a problem with this code:

if (90 >>= angle =<< 180)

The error explanation is:

The left-hand side of an assignment must be a variable.

I understand what this means but how do I turn the above code into correct code?

like image 859
matthijsW Avatar asked Feb 13 '14 17:02

matthijsW


People also ask

How do you check if a number is in between two values in Java?

To check if a number is between two numbers: Use the && (and) operator to chain two conditions. In the first condition check that the number is greater than the lower range and in the second, that the number is lower than the higher range.

How do you check if a value is between two values?

For example, you need to check if value in cell B2 is between values in cell A2 and A3. Please apply the following formula to achieve it. 1. Select a blank cell which you need to display the result, enter formula =IF(AND(B2>A2,B2<A3),"Yes","No") into the Formula Bar and then press the Enter key.

What is between operator in Java?

between() is a static method of the Range which is used to obtain an instance of Range with the specified minimum and maximum value. The specified minimum and maximum values are inclusive in nature. The method optionally takes in a custom Comparator for custom comparison logic.

How do you check if a number is between a range?

If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0. And must be smaller than or equal to high i.e., (high – x) <= 0. So if result of the multiplication is less than or equal to 0, then x is in range.


2 Answers

I see some errors in your code.
Your probably meant the mathematical term

90 <= angle <= 180, meaning angle in range 90-180.

if (angle >= 90 && angle <= 180) {

// do action
}
like image 132
AlexWien Avatar answered Oct 03 '22 08:10

AlexWien


You can use apache Range API. https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/Range.html

like image 20
Jay Avatar answered Oct 03 '22 08:10

Jay