java - 为什么java不让我有这个else if语句?

标签 java if-statement

我似乎不明白为什么java不允许我在while循环下的solve方法中使用else if语句,它告诉我这是一个语法错误。我认为在 else if 语句之后添加条件将是正确的语法,但它对我不起作用。

//Class to solve simple N Queens problem with backtracking, but without using recursion in a 1-d array.
    public class NQueens 
    {
        private int N;
        private int board[];
        public NQueens(int n)
        {
            this.N = n;
            this.board = new int[N];
            for(int i = 0; i<N; i++)
            {
                this.board[i]=-1;
            }
        }
        //checks the place for any attack queens in the same column, or diagnol
        public boolean safePlace(int row, int col, int [] board)
        {
            for (int i = 0; i<row; i++)
            {
                if((board[i] == col)|| (i-row)==(board[i]-col)|| (i-row)==(col-board[i]))
                    return false;
            }
            return true;
        }
        //solves N Queens problem using backtracking
        public void solve()
        {
            int row=0;
            while(row<this.N && row>-1)
            {   // condition that the row is empty and the queen is safe to place
                int col=0;
                if(this.board[row]==-1 && safePlace(row, col, this.board));
                {
                    this.board[row]=col;
                    row++;
                }   
                //condition that the row is not empty
                else if(this.board[row]>-1)
                {   
                    //start at column after the previous queen's column position. 
                    col=this.board[row-1]+1;
                    //checks for safety
                    if(safePlace(row, col, this.board))
                    {
                        this.board[row]=col;
                    }
                }
                else    //condition that no safe column can be found so queen in row is removed and we move back one row
                {
                    this.board[row]=-1;
                    row--;
                }
            }
            printBoard();
        }

        public void printBoard()
        {
                System.out.println("got to solve loop");
                for(int i = 0; i<N; i++)
                {
                    int chessBoard[]=new int[N];
                    chessBoard[this.board[i]] = 1;

                    if(chessBoard[i]==0)
                        System.out.print("* ");
                    else 
                        System.out.print("Q ");

                }
        }

        public static void main(String[] args)
        {
            NQueens q = new NQueens(4);
            q.solve();

        }
    }

最佳答案

if 语句中有一个分号,必须将其删除:

// semi-colon acts as an empty statement
if(this.board[row]==-1 && safePlace(row, col, this.board));

关于java - 为什么java不让我有这个else if语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29396462/

相关文章:

java - 以相反的顺序将文件转换为字节数组

JavaScript - 即使条件未满足也返回 true

r - 根据 R 中另一行的条件改变新列

python - 带输入检查的 If 语句输出不正确

python - 我该如何压缩循环,否则

java - 在 Java 中列出导入的正确样式是什么?

java - 使用 Java 和 Scribe 的 Vimeo 搜索 API

c# - 如果语句 ||或者 &&

java - 在 DAO 外部创建 JDBC 连接

java - spring mvc 应用程序中未填充选择框