【问题标题】:Creating an NxN matrix with parameters in the center [duplicate]创建一个带有中心参数的 NxN 矩阵[重复]
【发布时间】:2017-11-20 15:56:21
【问题描述】:

我在写这个方法时遇到了范围问题,我不确定如何确保在循环完成后可以返回在 wile 循环中构建的数组。该方法旨在获得一个偶数来创建一个黑白棋游戏板。 while 循环到位以确保电路板大小正确。将 return 语句放在循环中会导致错误。我的问题是如何返回在 while 循环中创建的数组。底部的 return 语句抛出一个 “找不到符号”错误。

    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

【问题讨论】:

  • 如果有的话你的问题不是很清楚。

标签: java multidimensional-array scope


【解决方案1】:

这是因为在代码块中创建的变量只存在于该块中,即:

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循环块之外声明变量才能访问它.

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

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

【讨论】:

  • 感谢您的反馈!这有助于我继续使用它。
【解决方案2】:

感谢您在上面的帮助,我能够使该方法起作用。这是带有建议更改的新方法。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多