Ineffective, but I think it doesn’t matter here: (I'm also a little rusty with Java, hopefully this is more or less the correct syntax.)
boolean isPositive = false; int n = (int)(x * x); while (n-- != 0) { if ((int)(--x) == 0) { isPositive = true; break; } }
This should work, because x will be decremented no more than x * x times (always a positive number), and if x never 0, then for starters it should be negative. If x , on the other hand, is 0 at some point, it should be behavioral.
Note that this will cause isPositive be false for 0.
PS: Admittedly, this will not work with very large numbers, since (int)(x * x) will overflow.
stakx Oct 22 '10 at 20:32 2010-10-22 20:32
source share