?
-
Is there a function to convert '1' into '-1' and vice versa?
- said:Is there a function to convert '1' into '-1' and vice versa?
- said:Is there a function to convert '1' into '-1' and vice versa?
Is there a function to convert '1' into '-1' and vice versa?
.. said:What datatype is '1' and '-1'? This is not proper notation for Java. The
use of single quotes denotes a char but the '-1' would be an invalid char.
So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
likely 1 to -1? The last option is very easy. i.e.
int n = 1;
n = -n;
.
What is it you are really trying to do?
.. said:What datatype is '1' and '-1'? This is not proper notation for Java. The
use of single quotes denotes a char but the '-1' would be an invalid char.
So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
likely 1 to -1? The last option is very easy. i.e.
int n = 1;
n = -n;
I love that! You're my hero!JScoobyCed said:public class Negate {
private Object lock = new Object();
public static int negate(int n) {
int tmp = 0;
synchronized(lock) {
tmp = n - (2 * n);
int loop = 0;
if(n > 0) {
loop = (new Random()).nextInt(n*1000);
for(int i=0; i<loop; i++) {
int j = i * (-1);
if((0 - j) == n) {
tmp = j;
break;
}
}
}
else {
loop = (new Random()).nextInt((-n)*1000);
for(int i=0; i<loop; i++) {
int j = i;
if((0 - j) == n) {
tmp = j;
break;
}
}
}
}
return tmp;
}
We can still find more complexe :-D
I love that! You're my hero!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.