Pregunta

Why does this statement does not work

boolean b = (y==3-x)||(y==3)||(y=3+x);

but this one does

boolean b = (y==3-x)||(y==3);
        b = b || (y == x-3);

and && statement has no problems with number of arguments passed

boolean b = x < 7 && x >= 0 && y < 7 && y >= 0;
¿Fue útil?

Solución

Because in the first case:

boolean b = (y==3-x)||(y==3)||(y=3+x);

You are doing the assignment not comparison for (y=3+x)

Change it to:

boolean b = (y==3-x)||(y==3)||(y==3+x);

and it will work for you

However in the second case:

boolean b = (y==3-x)||(y==3);
        b = b || (y == x-3);

You are doing the comparison everywhere thats why it is working for you!

Also in the third case you are doing comparison

boolean b = x < 7 && x >= 0 && y < 7 && y >= 0;

NOTE:-

== is for comparison and = is for assigment.

<,>,<=,>=, == are all used for comparison

Otros consejos

You missed an equals sign, meaning the last parenthesis assigns 3+xto b, evaluates to int rather than boolean and so can't be used for a logical OR expression. This works, though:

boolean b = (y==3-x)||(y==3)||(y==3+x);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top