2013-04-25 69 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!! 
} 
你的情況

現在,在這裏你什麼都的地方,你正在使用它定義庫爾斯,並在

  • 通知。
  • 揣摩應該在哪裏你定義庫爾斯變量。