Skip to content
Advertisement

Conversion table HTML/PHP/Java

I need to make a conversion table that essentially says if variable 1 is in this range and variable 2 is in this range then variable 3 = x. I’ve done this in PHP and it works, however for my current project PHP won’t work. Is there another straight forward way to accomplish this via Java or something? If anyone could just steer me in the right direction that’d be great!

here is a snipet of the working PHP version:

if(in_array($Var1, range(-10,-1))){

    if(in_array($Var2, range(0,1049))){
        $var3 =  "8";   
    }
    if(in_array($Var2, range(1050,1149))){
        $var3 =  "9";   
    }
    if(in_array($Var2, range(1150,1249))){
        $var3 =  "9";   
    }
    if(in_array($Var2, range(1250,1349))){
        $var3 =  "10";  
    }
    if(in_array($Var2, range(1350,1449))){
        $var3 =  "11";  
    }
    if(in_array($Var2, range(1450,1550))){
        $var3 =  "12";  
    }
}

if(in_array($cwr, range(-20,-11))){

    if(in_array($Var2, range(0,1049))){
        $var3 =  "9";   
    }
    if(in_array($Var2, range(1050,1149))){
        $var3 =  "9";   
    }
    if(in_array($Var2, range(1150,1249))){
        $var3 =  "10";  
    }
    if(in_array($Var2, range(1250,1349))){
        $var3 =  "11";  
    }
    if(in_array($Var2, range(1350,1449))){
        $var3 =  "12";  
    }
    if(in_array($Var2, range(1450,1550))){
        $var3 =  "13";  
    }
}

Advertisement

Answer

As suggested above, there is a much more efficient method to achieve this functionality. Rather than creating a new array with range() each time, we can simply test if the variable is within two bounds.

For example:

x = 12;
if(5 <= x && x <= 15){
    System.out.println("x in range");
}

Will print “x in range” because 12 is both greater than 5 and less than 15. As described by user above, you could turn this into a function:

boolean inRange(int x, int lower, int upper){
    return (lower <= x && x <= upper);
}

This code is simple and efficient, and should do exactly what you require!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement