0

I got the error: “bad operand types for binary operator <=“ and I’m not sure how to fix it. Can someone help me?

Hi! I’m a high school senior learning Java in class and I have a homework assignment where I have to complete quizzes and exercises online. I need help with a couple of exercises. Here’s the first one: “Write a method that takes 3 parameters and returns whether num is in between min and max inclusive” And here’s my code for it: public boolean inRange(int num, int min, int max){ if(min <= num <= max){ return false; } else{ return true; } }

17th Mar 2019, 4:42 AM
Marian Padayhag
Marian Padayhag - avatar
3 Respuestas
+ 17
[continued] 3) you can make 3C1 ie 3 independent conditions checking, like (X>=Y && X>=Z) if true then print X as greatest, make same 2 more conditions for Y & Z. [ 👉 this is the way you can get escape from the problem of non-associativity of <= operator]. //so total 2*3=6(both biggest & smallest) conditions you will be making if followed 3), you can read the ways I suggested if any doubt then can ask.
17th Mar 2019, 7:20 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 16
● In Java, some operators are non-associative & <= is one of them (so expression (a <b <c) is invalid)[source link https://introcs.cs.princeton.edu/java/11precedence/ (awesome source, for learning about precendence & associativity)] (this point for the error in code, another point will be for solving problem). ● so you need to think of some other way of finding smallest & largest value among those 3 numbers, there are many different ways it can be solved : 1)most easy way would be to put those numbers in an array & use .sort() function to sort array & take out first & last element. 2)you can also use if else statement for finding smallest & biggest number, you can try by yourself as I am giving basic way to think for it(for finding largest one): lets numbers be X, Y, Z, check if X>Y if TRUE { then check X>Z(if it is true then print X as largest) else print Z as largest} else{ //Y>X check if Y>Z (if it is true then print Y as largest) else print Z as largest)}.
17th Mar 2019, 7:01 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
0
bad operand types for binary operator '*'. Can someone please help me to fix it?
9th Jan 2021, 6:46 AM
MD. Alif Sheakh
MD. Alif Sheakh - avatar