简体   繁体   中英

Java <identifier> expected error?

I get 20 errors in middle for loops when compiling this program; 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 (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 ):

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.

Remove the int before b .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM