[英]Java <identifier> expected error?
I get 20 errors in middle for loops when compiling this program;编译这个程序时,我在中间的 for 循环中有 20 个错误; the following is only a snippet:
以下只是一个片段:
public static long[] bishopsMasks()
{
long[] masks = new long[64];
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
long x = 0L;
for (int a = i + 1, int b = j + 1; a < 7 && b < 7; a++, b++)
x |= bit(a, b);
for (int a = i + 1, int b = j - 1; a < 7 && b > 0; a++, b--)
x |= bit(a, b);
for (int a = i - 1, int b = j + 1; a > 0 && b < 7; a--, b++)
x |= bit(a, b);
for (int a = i - 1, int b = j - 1; a > 0 && b > 0; a--, b--)
x |= bit(a, b);
masks[i + j * 8] = x;
}
}
return masks;
}
I just can't find anything wrong with it!我只是找不到任何问题!
You can't declare multiple variables in a for-loop initializer like this:您不能像这样在 for 循环初始化程序中声明多个变量:
for (int a = i + 1, int b = j + 1; a < 7 && b < 7; a++, b++)
You can, however, do this (note the removal of int
before b
):但是,您可以这样做(注意在
b
之前删除int
):
for (int a = i + 1, b = j + 1; a < 7 && b < 7; a++, b++)
However, that means the variables have to be the same type, of course.但是,这意味着变量当然必须是相同的类型。
See the Java language specification section 14.14.1 for more information.有关详细信息,请参阅Java 语言规范第 14.14.1 节。
Remove the int
before b
.删除
b
之前的int
。
public static long[] bishopsMasks()
{
long[] masks = new long[64];
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
long x = 0L;
for (int a = i + 1, b = j + 1; a < 7 && b < 7; a++, b++)
x |= bit(a, b);
for (int a = i + 1, b = j - 1; a < 7 && b > 0; a++, b--)
x |= bit(a, b);
for (int a = i - 1, b = j + 1; a > 0 && b < 7; a--, b++)
x |= bit(a, b);
for (int a = i - 1, b = j - 1; a > 0 && b > 0; a--, b--)
x |= bit(a, b);
masks[i + j * 8] = x;
}
}
return masks;
}
When declaring multiple variables at once, you should not repeat the datatype.一次声明多个变量时,不应重复数据类型。
int a = i + 1, b = j + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.