2013-04-25 71 views
0

当编译这个静态方法能拿出符合整型数组变量,坐标,不能被发现的错误。我在方法中声明它,它的类型为int [],我不知道为什么它不起作用。我有一种感觉,它与静态方法有关,但将其改为静态是我发现使该方法首先运行的唯一方法。的java找不到符号错误:初学者

我觉得这可能是任何人很简单,但我特别是当所有我能找到的关于这个问题是更加复杂的编码问题。

在情况下,这有助于..此方法应该返回的(X,Y),用于移动位置坐标。对不起,可能没有正确输入代码。第一次这样做。在此先感谢您的帮助

CODE:

public static int[] getMove(String player) 
{ 
    boolean done = false; 
    while(!done) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Input row for " + player); 
     int x = in.nextInt() - 1; 
     System.out.println("Input column for " + player); 
     int y = in.nextInt() - 1; 
     int[] coord = {x,y}; 
     if(getLoc(coord[0], coord[1]).equals("x") || getLoc(coord[0], coord[1]).equals("o") || coord[0] < 0 || coord[0] > ROWS || coord[1] < 0 || coord[1] > COLUMNS) 

     { 
      System.out.println("Invalid coordinates... please retry"); 
     } 
     else 
     { 
      done = true; 
     } 
    } 
    return coord; 
} 
+0

coord在while循环中声明.............如果你想通过ur方法返回coord请在while循环之上声明... – hayat 2013-04-25 05:13:06

回答

1

这是因为数组coord是本地while循环。因此在其范围之外不可见。将coord的声明移至while之外,它应该可以工作。

int[] coord = new int[2]; 
while(!done){ 
    ... 
    ... 
    coord[0] = x; 
    coord[1] = y; 
    ... 
} 
+0

谢谢!我知道这会是一件简单的事情。 – user2318198 2013-04-25 05:14:48

2

你缺少什么是可变的范围。在父块中声明的变量可以在子块中访问,但不能以其他方式访问。

public void someMethod() 
{ 
int x=1; 

while(x<10) 
{ 
x++; //x is accessible here, as it is defined in parent block. 
int j = 0; //This variable is local to while loop and will cause error if used inside method 
j++; 
} 
System.out.println(j);// The outer block does not know about the variable j!! 
} 
你的情况

现在,在这里你什么都的地方,你正在使用它定义库尔斯,并在

  • 通知。
  • 揣摩应该在哪里你定义库尔斯变量。