How can this code be simplified?
if (x == 0) x = 1;
else if (x == 1) x = 0;
else if (x == 2) x = 3;
else if (x == 3) x = 2;
If x is always between 0 and 3 then try this:
x ^= 1;
It toggles the least significant bit.
If x can be a value other than between 0 to 3 then you can first test for that:
if (x >= 0 && x <= 3) {
x ^= 1;
}
This is the simplest form possible:
if (x == 0) x = 1;
else if (x == 1) x = 0;
else if (x == 2) x = 3;
else if (x == 3) x = 2;
wait... that's exactly your code.
cryptic one liners are NOT simple.
You could use something like this:
int mymap[4] = {1,0,3,2};
and then in your code use this:
x = mymap[x];
To use your pseudocode notation, maybe:
if (x % 2 == 0) x = x + 1
else x = x - 1
e.g you are adding one if it is an even number, subtracting otherwise? In terms of optimization though, I don't see what is particularly slow or wrong with your original code.
if(x >= 0 && x <= 3)
if((x%2) != 0) //odd
x--;
else if((x%2) == 0) //even
x++;
Not that I think this is simpler, and it doesn't limit the case to 0..3, but:
x += (x % 2 == 0) ? 1 : -1;
x ^= x & ~3 == 0 ? 1 : 0;
Unfortunately my code was so simplified it failed to make the 30-character minimum...
A common approach for handling simple data like this is to use the switch statement.
The code would be more redable with a switch statement:
switch(x) {
case 0: x=1: break;
case 1: x=0: break;
case 2: x=3: break;
case 3: x=2; break;
}
However, it's just about code readbility, not algorithmics, nor optimization.
x^=1;
unless x can be lower than 0 or higher than 3, which the problem specification doesn't state.
if( 0 <= x && x <= 3 )
x ^= 1;
if ( x >>> 2 == 0 )
{ x ^= 1;
}
one liner:
x=(x==0)?1:((x==1)?0:(x==2)?3:(x==3)?2:x);
The best way to do this...
if(x % 2 == 0){
x = +x;
}else{
x = -x;
}
Probably using switch statements
Related
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)) {
...
}
Hi guys i currently have a assignment which i just finished but their is one detail i dont love about it. Is there a way to shorten if else loops
Currently i have wrote
if (x >=300) {
set y = 1;
}
else if(x >=200) {
set y = 2;
}
else if (x >=150) {
set y = 3;
}
else if (x>=100) {
set y = 4;
}
else if (x >=50) {
set y = 5;
}
else if (x >=25) {
set y = 6;
}
Probably me just being pedantic, thanks in advance
You could shorten it to
y = x>=300 ? 1 : x>=200 ? 2 : x>=150 ? 3 : x>=100 ? 4 : x>=50 ? 5 : 6;
but while that may be more compact, it is also subjectively less readable. For additional informatione, see here.
This looks like a place where you could use the switch statement. However, the switch statement is designed to handle known values rather than inequalities. If you don't like the way the chain of if-else statements looks, you could do it all in an inline expression (Ternary Operator), but that makes it hard to read.
I'd say keep the code the way it is unless there's a good reason to change it. If you're only going to have one code statement after each statement, then you can eliminate the curly braces {} to make the code look a little cleaner:
if (x >= 300) set y = 1;
else if (x >= 200) set y = 2;
else if (x >= 150) set y = 3;
else if (x >= 100) set y = 4;
else if (x >= 50) set y = 5;
else if (x >= 25) set y = 6;
I tried searching stackoverflow for answer but i could not find it. i figured out when i remove part of the program that divides numbers:
if(znak == 0) {
Resenje = x + y;
}else if(znak == 1) {
Resenje = x - y;
}else if(znak == 2) {
Resenje = x/y;
} else if(znak == 3) {
Resenje = x*y;
}else {
System.out.println("Greska u programu");
}
that error does not appear. i think the problem maybe if number is float but it is stored in int... Thank you, if you need any additional information im here to provide it.. :)
PS code is messy because i made it long time ago.. sorry, i cant figure out how to properly format it, i posted it on pastebin, i hope you dont mind it.. :)
https://pastebin.com/sfG9JEbR link for code
while(vece = true) {
// System.out.println(Odabir1);
x = random.nextInt(Odabir1);
// System.out.println(x);
y = random.nextInt(Odabir1);
// System.out.println(y);
//if(x == (int)x) {
if(x-y >= 0 && x+y <= Odabir1 && x+y!=0 && x-y!=0 && x/y >= 0 && x/y == (int)x && x*y >= 0 && x*y <= Odabir1) {
System.out.println(x + " " + y);
break;
}
}
when i try adding && y == 0; i get same error in every possibility
My friend wrote this code for an assignment in his programming class:
public class test {
public static void main(String args[]) {
double x = 0.9;
double y = 0.1;
boolean truth = x < 1 && x > 0 && y < 1 && y > 0;
System.out.println(truth);
}
}
I'm wondering (for myself) if there's a way to simplify the conditional operators in this line specifically:
boolean truth = x < 1 && x > 0 && y < 1 && y > 0;
Your only option for a one-liner is to use parenthesis. Personally, I prefer multiple statements to make things much clearer:
boolean isXInRange = x > 0 && x < 1;
boolean isYInRange = y > 0 && y < 1;
boolean truth = isXInRange && isYInRange;
No, but it might be made clearer (opinion):
boolean truth = (0 < x && x < 1 && 0 < y && y < 1);
The flipping of the zero check makes it easy to read as 0 < x < 1. Is that clearer? A very little bit.
The parenthesis is a style choice. Since boolean expressions are always parenthesized in if statements and while loops, I find it clearer to always parenthesize boolean operators.
My suggestion for Java:
public boolean betweenExclusive(double start, double end, double val) {
return val > start && val < end;
}
and then:
boolean truth = betweenExclusive(0, 1, x) && betweenExclusive(0, 1, y);
or a little bit fancier ;)
boolean truth = Stream.of(x, y).allMatch(x => betweenExclusive(0, 1, x));
I am simply combining two answers (#Andreas and #Justin Niessner) here, so real credit goes to them.
boolean isXInRange = 0 < x && x < 1;
boolean isYInRange = 0 < y && y < 1;
boolean truth = isXInRange && isYInRange;
Hope this helps!
I need to set an int variable in each iteration +-1, the range should be between 0-10. When i add 1 to 10 -> 0 when i add -1 to 0 -> 10. I know i need to go with modulo here but cannot find any solution.
This seems to easy, but if you really want to use modulo, did you try;
x = (x + y) % 11;
if (x < 0) x += 11;
or for "ultimate readability" and probably still better performance just
x = x + y;
if (x < 0) x += 11;
if (x > 11) x -= 11;
Please note that the requirements locks y down to being -1 or 1.