How to convert '1' into '-1' and vice versa?

J

JScoobyCed

- said:
Is there a function to convert '1' into '-1' and vice versa?

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
 
?

.

Is there a function to convert '1' into '-1' and vice versa?

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;
 
K

Knute Johnson

.. 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?
 
R

Roland

.

What is it you are really trying to do?

I don't think that OP ("-") is the same person you are replying to (".").
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
?

-

.. 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;

Indeed, the last option was what I wanted to do.

I was thinking along the line that since there is a Math.abs(double)
function, I thought there was a method to do what I wanted.

Didn't realise it was that easy. No wonder it doesn't warrant a method
of its own. :p
 
W

Wibble

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!
 
A

Antti S. Brax

I love that! You're my hero!

That is bullshit. If you're going to write a comedy response
for a trivial question, at least make it work properly.

Hint 1: the code will fail miserably if n is greater than
Integer.MAX_VALUE / 1000.

Hint 2: you can't access instance variables from static
context.

Also, it is trivial to make a complex example by adding
useless code (ie. the unnecessary synchronization block).
You might as well take the source code for Tomcat, embed
it inside your method and then return -n in the end.

If you need a hero in the field of comedy coding, please
read this: http://home.tiac.net/~cri/2001/badsort.html
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top