mercredi 26 août 2020

Is there a way to simplify this code which aims to return an absolute value given an integer?

Is there a way to simplify this piece of code further? The codes aim is to return an absolute value given an integer.

public class Abs {
public static int abs(int x) {
if(x < 0) { return -x; }
if(x >= 0) { return x; }
assert false;
return 0;
}
}

Aucun commentaire:

Enregistrer un commentaire