I have a use case where I have to check if one condition is subset of another condition. Meaning, the result set that satisfies condition 2 will be the subset of result set that satisfies condition 1.
I was checking and intellij does figures it out during code inspection
if (x < 7 && x > 1) {
return true;
} else if (x > 3 && x <6) {
return false;
}
Here during code inspection it does highlights that result of second condition is always false.
So, How it is implemented?
This range:
(x > 3 && x <6)
is a subrange of:
(x < 7 && x > 1)
so if the condition (x < 7 && x > 1)
is true then this condition (x > 3 && x <6) will not be reached
is false then this condition (x > 3 && x <6) is also false
So the meaning of code inspection is:
if (x > 3 && x <6) is reached then it is always false.
The right way to implement your logic should be to check first the subrange:
if (x > 3 && x <6) {
return false;
} else if (x > 1 && x < 7) {
return true;
} else {
return ....;
}
Related
Why is it when I do this
while(word.charAt(left) == word.charAt(right) && left >= 0 && right < word.length()){
left--;
right++;
}
its says in the while conditions "left >= 0 && right < word.length()" condition doesn't reach, but when I do it like this
while(left >= 0 && right < word.length() && word.charAt(left) == word.charAt(right)){
left--;
right++;
}
The conditions are met.
Is there an order for while(loop conditions)???
The order does matter as conditions are checked from left to right. You're getting that message because if the previous condition evaluates to true, then those two conditions
left >= 0 && right < word.length()
will always evaluate to true. You probably want the latter (since you would want to check all the conditions).
I've recently learnt ternary operators and was practising them by making some old code i wrote a while back nicer. When trying to do this to a for loop in many different ways I can't seem to figure out how to do it. Ive tried:
for (hotbarFirst ? (x = 0; x < mc.player.inventoryContainer.getInventory().size(); x++) :
(x = mc.player.inventoryContainer.getInventory().size(); x > 0; x--)) {
and
for (hotbarFirst ? (x = 0) : (x = mc.player.inventoryContainer.getInventory().size());
hotbarFirst ? (x < mc.player.inventoryContainer.getInventory().size()) : (x > 0);
hotbarFirst ? (x++) : (x--)){
}
The first way gives me unexpected token errors and the second one gives me not a statement errors. It seems like I should be able to do this in some way or another, so am I just approaching it wrong or is there another way to do this without making two for loops.
(ignore the functions, they're for a game I made the mod in)
(Also incase you didnt notice I'm trying to iterate over a set of numbers two either back to front or front to back depedning on whether the bool is true or false)
original code:
public static int getItem(Item itemofChoice, boolean hotbarFirst) {
if (mc.player == null) return -1;
for (int x = 0; x < mc.player.inventoryContainer.getInventory().size(); x++) {
if ((x == 0 || x == 5 || x == 6 || x == 7 || x == 8)) continue;
ItemStack s = mc.player.inventoryContainer.getInventory().get(x);
if (s.isEmpty()) continue;
if (s.getItem().equals(itemofChoice)) return x;
}
return -1;
}
Im trying to make it iterate the opposite way if the bool param is true
Here is one way to do it:
int size = mc.player.inventoryContainer.getInventory().size();
for (int x = (hotbarFirst ? 0 : size-1); (hotbarFirst ? x < size : x >= 0) ; x += (hotbarFirst ? 1 : -1)) {
...
}
I'm learning to code in Java and I'm doing some online exercises where the answer is not explained too much, so I was just curious why my code is incorrect when it seems to be similar to the solution.
The exercise says -
"Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative."
public boolean posNeg(int a, int b, boolean negative) {
if (negative && (a < 0 && b < 0)) {
return true;
}
return (a < 0 && b > 0 || a > 0 && b < 0);
} // This is my code that yields unwanted results
public boolean posNeg(int a, int b, boolean negative) {
if (negative) {
return (a < 0 && b < 0);
}
else {
return ((a < 0 && b > 0) || (a > 0 && b < 0));
}
} // This is the solution code
When running posNeg(-4, 5, true); it comes out to be true even though it is supposed to be false. Whenever one int is negative and the other is positive and negative is true, it is supposed to be false but yields true.
public boolean posNeg(int a, int b, boolean negative) {
if (negative && (a < 0 && b < 0)) {
return true;
}
return (a < 0 && b > 0 || a > 0 && b < 0);
} // This is my code that yields unwanted results
Calling posNeg(-4, 5, true); makes the first condition false negative && (a < 0 && b < 0) <===> true && (true && false) <===> false.
Then the run jumps to the end of that if and evaluates the last condition (a < 0 && b > 0) || (a > 0 && b < 0) which is obviously true.
I am working on making a coffee ordering system. It takes inputs from users and calculates the price of their coffee. One of the inputs asked for is "syrupshots". This code always makes syrupshots = 3 even if the user properly chose a number according to the size of their coffee.
//if size == tall syrup shots <=3
if(size == 1 && syrupshots < 1 || syrupshots > 3)
{
syrupshots = 3;
}
//else if size == medium syrup shots <=5
else if(size == 2 && syrupshots < 1 || syrupshots > 5)
{
syrupshots = 3;
}
//else if size == Venti syrup shots <=7
else if(size == 3 && syrupshots < 1 || syrupshots > 7)
{
syrupshots = 3;
}
System.out.println(syrupshots);
I am not sure why syrupshots is always = 3 no matter what.
Not always, sometimes it returns 1 or 2. But never >3, like it looks like it should.
Your problem is with logical operator precedence. For example:
if(size == 1 && syrupshots < 1 || syrupshots > 3) {}
If size=3 and syrupshots=5 (a valid combination), then this if block is still entered because the && is evaluated first:
size == 1 && syrupshots < 1
and that equals false, so you have left (false || syrupshots > 3)
BUT then the || is evaluated, syrupshots > 3 is true, so the whole expression is true
You need to change the order of precedence by using brackets:
if(size == 1 && (syrupshots < 1 || syrupshots > 3)) {}
As one comment says, all conditions trigger syrupshots to be assigned three. Also, I don't think you wrote your test condiiotns correctly, judging from your comments.
if(size == 1 && syrupshots <= 3)
^That would be proper way to test for the check outlined in your comment
if(pl.y+pl.height >= a.y && pl.x+pl.width >= a.x+1 && pl.x <= a.x+a.width-1 && pl.y<=a.y) { //TOP
colUP=true;
}
else colUP=false;
if(pl.y <= a.y+a.height && pl.x+pl.width >= a.x+1 && pl.x <= a.x+a.width-1 && pl.y+pl.height>=a.y+a.height) { //BOTTOM
colDOWN=true;
}
else colDOWN=false;
if(pl.x <= a.x+a.width && pl.x+pl.width>a.x+a.width && pl.y+pl.height >= a.y && pl.y <= a.y+a.height){ //RIGHT
colRIGHT=true;
}
else colRIGHT=false;
if(pl.x+pl.width >= a.x && pl.x<a.x && pl.y+pl.height >= a.y && pl.y <= a.y+a.height){ //LEFT
colLEFT=true;
}
else colLEFT=false;
I setup a debug that will tell me which of the 4 Booleans is being set to true, and they don't show that when I put the box 'pl' on top of box 'a' colUP is not equal to true, and they will only come true in weird instances where box 'pl' is colliding with several box 'a's , and the collision for a certain side might be true when it isn't but if colUP is true then colRIGHT is true for some reason. (This code is inside a for loop that goes through an array list of Rectangles and sets the current Rectangle equal to the variable 'a' so that a.x is the box's x position)
You have right logic but you set false for each condition separately. In reality all conditions should be true. So, use one boolean variable - isInRectangle=true; then check all conditions - left,right,top,bottom. If any is not true then isInRectangle=false;
It is simple AND logical operation for all 4 conditions.