私のプログラムが同じステートメントを繰り返し出力している理由を理解しようとしています。適切な値を入力すると、このメソッドを正常に機能させることができますが、無効な選択をキャッチするようにelseステートメントを設定しています。elseステートメントを実行すると、無限に出力され、whileループが開始されることはありません。私はそれを理解することはできません。メソッド全体を貼り付けますが、問題のある領域を太字にします。これが明確であることを願っています。
public static int[][] placeCheese(int [][] gameBoard, String Player1Name)
{
Scanner console = new Scanner(System.in);
int turnTaker = 0;
int validRow = 0;
int validCol = 0;
int RowIndex = -1;
int ColIndex = -1;
while(turnTaker == 0)
{
while(validRow == 0)
{
System.out.println( Player1Name + ", please place your cheese by choosing a row, or choose -1 to exit.");
RowIndex = console.nextInt() -1;
if(RowIndex < gameBoard.length && RowIndex >=0)
{
validRow++;
}
else if(RowIndex == -2)
{
System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
System.exit(0);
}
}
while(validCol == 0){
System.out.println( Player1Name + ", please place your cheese by choosing a column, or choose -1 to exit.");
ColIndex = console.nextInt() -1;
if(ColIndex < gameBoard.length && ColIndex >=0)
{
validCol++;
}
else if(RowIndex == -2)
{
System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
System.exit(0);
}
}
if(gameBoard[RowIndex][ColIndex] == 0)
{
gameBoard[RowIndex][ColIndex] = 1;
turnTaker++;
numPieces1--;
}
else
{
System.out.println("this space is already occupied, please choose again.");
}
return gameBoard;
}