java - non-static method count(int) cannot be referenced from a static context -


this question has answer here:

this n queens problem try solve ,but have problem of non-static method .. how can solve .....

*> @ count(int) method .. don't understand how solve problem

error: non-static method count() cannot referenced static context

import java.util.*;   public class nqueens { static int n; int[][] board = new int[n][n];  public static void main(string[] args) {      //int result;     int col = 0;     scanner input=new scanner(system.in);     n=input.nextint();     if(n < 4)         system.out.println("the result 0");     else         count(0);  }  int cnt=0; void  count(int col){     if(col == n){         cnt++;         system.out.println("the result " + cnt);     }      else{         for(int row=0 ; row<n ; row++){             if(placequeen(row, col))                 count(col+1);             else                 removequeen(row , col);         }     }  }  boolean placequeen(int row , int col){     boolean x =false;     if(validplace(row , col)){         setqueen(row , col);         x = true;     }     return x; } boolean validplace(int row ,int col){     boolean x = false;     if(board[row][col] != -1)         x=true;     return x; } void setqueen(int row , int col){     board[row][col] = 1;     killcell(row , col); } void killcell(int row , int col){     for(int i=col+1 ; i<n ; i++)         board[row][i] = -1;     for(int k=col+1 ; k<n ; k++){         for(int j=0 ; j<n ; j++)             if(math.abs(row-j) == math.abs(col-k))                 board[j][k] = -1;     } } void removequeen(int row , int col ){     board[row][col] = 0;     refreshcell(row , col); } void refreshcell(int row , int col){     for(int =col+1 ; i<n ; i++)         board[row][i]=0;     for(int k=col+1 ; k<n ; k++){         for(int j=0 ; j<n ; j++)             if(math.abs(row-j) == math.abs(col-k))                 board[j][k]=0;     } } 

}

your main method static method , has no instance of class until creates one. cannot call count(int) method unless have instance of class. try instead:

public static void main(string[] args) {      //int result;     int col = 0;     scanner input=new scanner(system.in);     n=input.nextint();     if(n < 4)         system.out.println("the result 0");     else {         nqueens nq = new nqueens();         nq.count(n);     }  } 

another problem count(int) method tries refer 'n' variable. cannot because it's existing in static main method. have pass 'n' count method can see it. if want initialize class starting value, perhaps need constructor takes value instead, this:

public void nqueens(int starting_n) {     start = starting_n; } 

then in main you'd use nqueens nq = new nqueens(n); create object. you'd have define int start = 0; in class has place store value being passed in.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -