Every time I call myGrid
in a method generateBoard
, I get an error:
non-static variable myGrid
cannot refer to static context
As far as I understand, this should not happen, because I set the array to public and must be accessible from any other class. So I misconfigured the array?
import java.util.Random;
public class Zombies {
private int Level = 1;
private int MoveNo = 0;
public int[][] myGrid = new int[12][12];
public static void generateBoard() {
Random rand = new Random();
int i, j;
for (i = 0; i < 12; i++) {
for (j = 0; j < 12; j++) {
if ( i == 6 && j == 6) {
myGrid[i][j] = 'P';
}
if (rand.nextInt(4) == 0) {
myGrid[i][j] = 'I';
}
myGrid[i][j] = 'x';
}
}
}
public static String printBoard() {
int i, j;
for (i = 0; i < 12; i++) {
for (j = 0; j < 12; j++) {
if (j == 0) {
System.out.print( "| " );
}
System.out.print( myGrid[i][j] + " " );
if (j == 12) {
System.out.print( "|" );
}
}
}
}
}
source
share