使用中心

时间:2017-11-20 15:56:22

标签: java multidimensional-array scope

我有这个方法的范围问题我写的不知道如何确保在循环完成后可以返回构建在wile循环中的数组。该方法旨在获得偶数来创建一个Reversi游戏板。 while循环适用于确保电路板尺寸正确。将return语句放在循环中会导致错误。我的问题是如何返回在while循环中创建的数组。底部的返回语句是抛出一个 “找不到符号”错误。

    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 个答案:

答案 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");

在此代码中,变量标志不能在最后一个if语句中访问,因为它不存在于for循环之外,因此您必须在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");

答案 1 :(得分: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