-2

I'm having a scope issue with this method I'm writing not sure how to ensure that array built in the wile loop can be returned once the loop is finished. The method is designed to get an even number to create a Reversi game board. The while loop is in place to ensure the board size is correct. Placing the return statement in the loop causes an error. My question is how to do return my array created in the while loop. The return statement at the bottom is throwing an "cannot find symbol" error.

    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

2 Answers2

0

that's because variables created inside a code block only lives in that block i.e:

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

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

in this code the variable flag can't be accessed in the last if statement because it doesn't exist outside the for-loop, so you must declare the variable in outside the for-loop block to be able to access it.

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

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

Thank you for your help above I was able to get the method to work. Here is the new method with the suggested changes.

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