简体   繁体   中英

How to fix the error "Bad Operand Types for Binary Operator '>=' first type: int[] second type int"

**The error is with this line: **

if ((board[r + vertical[movenumber]]) <= 8 && board[r + vertical[movenumber]] >= 1)

**Whole method if needed: **

public void tour()
{

    int starter = 1;

    int start1 = (int)(Math.random() * 8 - 1) + 1;
    int start2 = (int)(Math.random() * 8 - 1) + 1;

    board[start1][start2] = starter;

    int r = start1;
    int c = start2;

    for (int count = 0; count < board[row].length; count++)
    {

        numb[count] = count;

    }

    for (int runs = 2; runs <= 64; runs++)
    {

        int movenumber = (int)(Math.random() * 8 - 1) + 1;

        if ((board[r + vertical[movenumber]]) <= 8 && board[r + vertical[movenumber]] >= 1)
        {

            if (board[r + vertical[movenumber]][c + horizontal[movenumber]] == 0)
            {

                board[r + vertical[movenumber]][c + horizontal[movenumber]] = runs;

                // System.out.println(r + "," + c);

                r = r + vertical[movenumber];
                c = c + horizontal[movenumber];

            }

        }

    }

}

The error is pretty specific - you are trying to compare an array with an int . The board[r + vertical[movenumber]] expression is an array (or int[] , specifically), because your board is int[][] . To make it an int , you need to add a second index, just like you do in the following lines: board[r + vertical[movenumber]][c + horizontal[movenumber]] .

And from quick glance at the code, I think it should be r + vertical[movenumber] <= 8 instead.

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