2017-11-20 2 views
-2

この方法でスコープの問題が発生しています。ループが終了したら、wileループで構築された配列を確実に返す方法がわかりません。このメソッドは、Reversiゲームボードを作成するために偶数番号を取得するように設計されています。 whileループは、ボードのサイズが正しいことを保証するために配置されています。 returnステートメントをループ内に配置すると、エラーが発生します。私の質問は、whileループで作成された配列を返す方法です。下部のreturnステートメントは "シンボルを見つけることができません"というエラーを投げています。中心にパラメータを持つNxN行列を作成する

コードブロック内で作成された変数はそのブロックのみ、すなわちに住んでいるためだ
public static int [][] GetGameBoard(){ 

     //initalizing local. 
     int sizeChoice = 1; 
     int black = 1; 
     int white = 2; 


     //creating scanner object. 
     Scanner input = new Scanner(System.in); 

     //while loop to ensure choice is correct. 
     while((sizeChoice%2) != 1 && sizeChoice < 4){ 
      //asing user for input. 
      System.out.println("Please enter the size of the board you want!"); 
      System.out.print("The size must be > 4 and an even number: "); 
     System.out.println("\n"); 


     sizeChoice = input.nextInt(); 

     if (sizeChoice%2 == 0 && sizeChoice >= 4){ 
      System.out.println("You have chosen a " + sizeChoice + "x" 
      + sizeChoice + " board."); 
      //creating board. 
      int theBoard[][] = new int[sizeChoice][sizeChoice]; 
      //intializing half way points for board center 
      int halfWay1 = (sizeChoice/2); 
      int halfWay2 = (sizeChoice/2) + 1; 
      theBoard[halfWay1][halfWay1] = black; 
      theBoard[halfWay1][halfWay2] = white; 
      theBoard[halfWay2][halfWay1] = white; 
      theBoard[halfWay2][halfWay2] = black; 
     } 
     else if(sizeChoice%2 != 0){ 
      System.out.println("Incorrect input you must choose an even," 
        + "number!"); 
     } 
     else if (sizeChoice < 4){ 
      System.out.println("Incorect size you must choose a size board" 
        + " >= 4."); 
     }//end of if/else's 
    }//end of while. 
    return theBoard; 
}// end of GetGameBoard 
+0

あなたの質問がある場合、あなたの質問はあまり明確ではありません。 –

答えて

0

:このコードで

for(i=0; i<n; i++){ 
    boolean flag; 
    if(arr[i] % 2 == 0) 
     flag = true; 
} 

if(flag) 
    System.out.println("I'm alive"); 

ことがないので、変数フラグがあれば、最後の文でアクセスすることはできませんfor-loopの外側に存在するので、for-loopブロックの外側に変数を宣言して、そのループにアクセスできるようにする必要があります。

boolean flag; 
for(i=0; i<n; i++){ 
    if(arr[i] % 2 == 0) 
     flag = true; 
} 

if(flag) 
    System.out.println("I'm alive"); 
+0

フィードバックをありがとう!それは私がそれを続けていくのを助けました。 –

0

上記のご協力をいただき、ありがとうございました。私はこのメソッドを動作させることができました。提案された変更を加えた新しいメソッドがあります。

public static int [][] GetGameBoard(){ 

     //initalizing local. 
     int sizeChoice = 1; 
     int black = 1; 
     int white = 2; 

     //creating scanner object. 
     Scanner input = new Scanner(System.in); 

     //asking for gameboard size 
     System.out.println("Please enter an even number greater or equal to" 
       + " 4 for the game board size: "); 

     sizeChoice = input.nextInt(); 

     //while loop to ensure choice is correct. 
     while((sizeChoice%2) != 0 || sizeChoice < 4){ 
      //asing user for input. 

      if(sizeChoice%2 != 0 && sizeChoice > 2){ 
      System.out.print("Incorrect input you must choose an even," 
        + "number!"); 
      } 
      if (sizeChoice < 4){ 
       System.out.print("Incorect size you must choose a size " +" 
       board"+ " >= 4."); 
      } 
      if ((sizeChoice%2) != 0 || sizeChoice < 4){ 
       System.out.print("Please try again: "); 
      }//end of if/else's 

      sizeChoice = input.nextInt(); 

     }//end of while. 

     if (sizeChoice%2 == 0 && sizeChoice >= 4){ 
      System.out.println("You have chosen a " + sizeChoice + "x" 
      + sizeChoice + " board."); 
      }//end of if 

     //creating board. 
     int theBoard[][] = new int[sizeChoice][sizeChoice]; 
     //intializing half way points for board center 
     int halfWay1 = (sizeChoice/2) - 1; 
     int halfWay2 = (sizeChoice/2); 
     theBoard[halfWay1][halfWay1] = black; 
     theBoard[halfWay1][halfWay2] = white; 
     theBoard[halfWay2][halfWay1] = white; 
     theBoard[halfWay2][halfWay2] = black; 

     return theBoard; 
    }// end of GetGameBoard 
関連する問題