【发布时间】: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