Author |
Message |
wicherh
Joined: 19 Dec 2008 Posts: 3
|
|
Valid java code |
|
This is a pretty stupid question but a equation example: (1 = 1) returns 1 or 0 right so why would this not work?:
 |
 |
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:
 |
 |
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]";" expected[code]
why does he ask that
|
|
Fri Dec 19, 2008 9:39 pm |
|
 |
tog
Joined: 14 Nov 2008 Posts: 70 Location: Germany |
|
|
|
= 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.
|
|
Fri Dec 19, 2008 10:31 pm |
|
 |
brazzy
Joined: 07 Nov 2008 Posts: 14 Location: Munich, Germany |
|
Re: Valid java code |
|
 |
 |
This is a pretty stupid question but a equation example: (1 = 1) returns 1 or 0 right so why would this not work?:
 |
 |
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.
|
|
Fri Dec 19, 2008 11:53 pm |
|
 |
wicherh
Joined: 19 Dec 2008 Posts: 3
|
|
|
|
Yea got right code now with modulo.
If you are at a if statement there stays example
 |
 |
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
|
|
Sat Dec 20, 2008 10:22 am |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|