Valid java code

Post Reply
wicherh
Posts: 3
Joined: Fri Dec 19, 2008 7:50 pm

Valid java code

Post by wicherh »

This is a pretty stupid question but a equation example: (1 = 1) returns 1 or 0 right so why would this not work?:

Code: Select all

int right = dir + 1 - 4(dir = 4); 
int left = dir - 1 + 4(dir = 0);
This code calculates right and left dir so i can use after this the code:

Code: Select all

public static int[][] offsets = { { 1, 0 }, { 0, -1 }, { -1, 0 }, { 0, 1 } };
int nx = x + offsets[dir][0];
int ny = y + offsets[dir][1];
int nxr = x + offsets[right][0];
int nyr = y + offsets[right][1];
int nxl = x + offsets[left][0];
int nyl = y + offsets[left][1];
if I use the first code, it says

Code: Select all

";" expected[code]
why does he ask that
tog
Posts: 70
Joined: Fri Nov 14, 2008 11:23 am
Location: Germany

Post by tog »

= is assignment
== is comparison
It's really a bad idea to assign to a variable which is used in the same statement again. You can never know which part of the statement is executed first.
But you actually wanna use modulo (%) for this task.
brazzy
Posts: 14
Joined: Fri Nov 07, 2008 2:30 am
Location: Munich, Germany
Contact:

Re: Valid java code

Post by brazzy »

wicherh wrote:This is a pretty stupid question but a equation example: (1 = 1) returns 1 or 0 right so why would this not work?:

Code: Select all

int right = dir + 1 - 4(dir = 4); 
int left = dir - 1 + 4(dir = 0);
Not sure what that code is supposed to do, but it's definitely invalid as long as there is no operator in front of the parentheses. Also, what tog said.
wicherh
Posts: 3
Joined: Fri Dec 19, 2008 7:50 pm

Post by wicherh »

Yea got right code now with modulo.
If you are at a if statement there stays example

Code: Select all

if (x = 1) 
{
}
X =1 returns a value boolean true or false, 1 or 0.
int right = dir + 1 - 4(dir = 4); means:

int right = dir + 1 - 4 * (if dir = 4(if = true it returns a 1 value)) then the equanation does it -3
Post Reply