Why wont java let me have this else if statement? -
i dont seem understand why java not allow me have else if statement in solve method under while loop, tells me syntax error. thought condition after else if statement correct syntax not working me.
//class solve simple n queens problem backtracking, without using recursion in 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 = 0; i<n; i++) { this.board[i]=-1; } } //checks place attack queens in same column, or diagnol public boolean safeplace(int row, int col, int [] board) { (int = 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 row empty , queen safe place int col=0; if(this.board[row]==-1 && safeplace(row, col, this.board)); { this.board[row]=col; row++; } //condition row not empty else if(this.board[row]>-1) { //start @ column after previous queen's column position. col=this.board[row-1]+1; //checks safety if(safeplace(row, col, this.board)) { this.board[row]=col; } } else //condition no safe column can found queen in row removed , move 1 row { this.board[row]=-1; row--; } } printboard(); } public void printboard() { system.out.println("got solve loop"); for(int = 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(); } }
you have semi-colon in if
statement must removed:
// semi-colon acts empty statement if(this.board[row]==-1 && safeplace(row, col, this.board));
Comments
Post a Comment