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; } }
3 Answers
+ 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.
+ 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)}.
0
bad operand types for binary operator '*'. Can someone please help me to fix it?