我正在开发一款简单的井字游戏4x4游戏。我有input()函数,用户输入他的数据,printBoard()打印板本身,analyzeBoard(),其中所有的计算选择赢家和完成()简单返回状态。我的问题是,即使我找到胜利者,我也不能停止输入()工作,并且它会一遍又一遍地保持印刷板。我第一次认为问题出现在analyzeBoard()中,但后来我发现它实际上返回了我需要的所有内容,问题出在input()中。当我尝试从输入中返回时,它或者说要丢失返回语句,或者即使它可以看到胜利者也不会停止。
public char input ()
{
while (status == false || numberOfMovesLeft > 0)
{
System.out.println("User "+ whoseTurn + " enter your move: "); //prompt user to make a move
char userInput = reader.next().charAt(0); // local variable to hold user input
// validation of user input
while (userInput != 'a' && userInput != 'b' && userInput != 'c'
&& userInput != 'd' && userInput != 'e' && userInput != 'f'
&& userInput != 'g' && userInput != '1' && userInput != '2'
&& userInput != '3' && userInput != '5' && userInput != '6'
&& userInput != '6' && userInput != '7' && userInput != '8'
&& userInput != '9')
{
System.out.println("Try again, user "+ whoseTurn + " enter your move: ");
userInput = reader.next().charAt(0);
}
// places user input into a board cell
for (int row = 0; row < board.length; row++)
{
for (int col = 0; col < board[row].length; col++)
{
if (board[row][col] == userInput)
{
board[row][col] = whoseTurn;
}
}
}
// check for tie result
numberOfMovesLeft--;
if (numberOfMovesLeft==0)
{
System.out.println("Tie!");
winner = 'T';
System.out.println(winner);
}
analyzeBoard();
printBoard();
done();
whoseTurn = (whoseTurn == 'X') ? '0' : 'X';
}
return winner;
}
public void analyzeBoard()
{
// row winner algoritm
for (int row = 0; row <=3; row++)
{
for (int col = 0; col < 2; col++)
{
if (board[row][col] == board[row][col + 1]
&&
board[row][col] == board[row][col + 2])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
System.out.println("status "+status);
done();
}
else
{
//System.out.println("status "+status);
}
}
}
//column winner algoritm
for (int row = 0; row <2; row++)
{
for (int col = 0; col <= 3; col++)
{
if (board[row][col] == board[row+1][col] &&
board[row][col] == board[row+2][col])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
//System.out.println("status "+status);
}
}
}
//diagonal winner algoritm
for (int row = 0; row <2; row++)
{
for (int col = 0; col <2; col++)
{
if (board[row][col] == board[row+1][col+1] &&
board[row][col] == board[row+2][col+2])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
//System.out.println("status "+status);
}
}
}
//diagonal winner algoritm
for (int row = 0; row <2; row++)
{
for (int col = 2; col <=3; col++)
{
if (board[row][col] == board[row+1][col-1])
{
System.out.println("winner");
winner = whoseTurn;
System.out.println("winner "+ winner);
System.out.println("whoseTurn "+ whoseTurn);
status = true;
//System.out.println("status "+status);
}
}
}
}
答案 0 :(得分:0)
您可以使用break;
来终止当前循环,或使用return;
来终止整个函数调用。