Check if a variable is between two numbers with Java

后端 未结 7 1972
暗喜
暗喜 2020-12-10 13:35

I have a problem with this code:

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

The error explanation is:

The left-hand side

相关标签:
7条回答
  • 2020-12-10 13:38

    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
    }
    
    0 讨论(0)
  • 2020-12-10 13:41
    //If "x" is between "a" and "b";
    
    .....
    
    int m = (a+b)/2;
    
    if(Math.abs(x-m) <= (Math.abs(a-m)))
    
    {
    (operations)
    }
    

    ......

    //have to use floating point conversions if the summ is not even;

    Simple example :

    //if x is between 10 and 20
    
    if(Math.abs(x-15)<=5)
    
    0 讨论(0)
  • 2020-12-10 13:41

    Assuming you are programming in Java, this works:

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

    (don't you mean 90 is less than angle? If so: 90 <= angle)

    0 讨论(0)
  • 2020-12-10 13:47

    <<= is like +=, but for a left shift. x <<= 1 means x = x << 1. That's why 90 >>= angle doesn't parse. And, like others have said, Java doesn't have an elegant syntax for checking if a number is an an interval, so you have to do it the long way. It also can't do if (x == 0 || 1), and you're stuck writing it out the long way.

    0 讨论(0)
  • 2020-12-10 13:47
    public static boolean between(int i, int minValueInclusive, int maxValueInclusive) {
        if (i >= minValueInclusive && i <= maxValueInclusive)
            return true;
        else
            return false;
    }
    

    https://alvinalexander.com/java/java-method-integer-is-between-a-range

    0 讨论(0)
  • 2020-12-10 13:54

    are you writing java code for android? in that case you should write maybe

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

    updating the code to a nicer style (like some suggested) you would get:

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

    now you see that the second check is unnecessary or maybe you mixed up < and > signs in the first check and wanted actually to have

    if (angle >= 90 && angle <= 180) {
    
    0 讨论(0)
提交回复
热议问题