gpt4 book ai didi

java - 代码仅在一种情况下不断跳过一行输入

转载 作者:行者123 更新时间:2023-11-30 05:22:28 25 4
gpt4 key购买 nike

我一直在为 TicTacToe 游戏编写代码,但我陷入了一个循环,当玩家为已标记的位置输入输入时,它会提示用户再次输入。问题是,这只是代码跳过一行输入的情况。我应该做什么?

   Scanner input = new Scanner(System.in);
boolean bool = false;

System.out.println("It is the player's move");
System.out.println("Give me your best move!");

while(!bool){
String Answer = input.nextLine();

Answer = validateUserResponse(Answer);

int move = Integer.parseInt(Answer);

if(validatePlayerMove(move))
{
if(checkPositionAvailability(move))
{
board[move] = 'H'; //make 'H' for player move
bool = true;
}
else
{
System.out.println("Position not available.\nMake a different choice.");
Answer = input.nextLine();
}
}
else
{
System.out.println("Invalid entry!");
Answer = input.nextLine();
}
}

最佳答案

所以我已经弄清楚了,我在 boolean 循环中提示了两个输入,所以我只需将第一个输入移到循环之外。

Scanner input = new Scanner(System.in);
boolean bool = false;

System.out.println("It is the player's move");
System.out.println("Give me your best move!");

String Answer = input.nextLine();

while(!bool){

Answer = validateUserResponse(Answer);

int move = Integer.parseInt(Answer);

if(validatePlayerMove(move))
{
if(checkPositionAvailability(move))
{
board[move] = 'H'; //make 'H' for player move
bool = true;
}
else
{
System.out.println("Position not available.\nMake a different choice.");
Answer = input.nextLine();
}
}
else
{
System.out.println("Invalid entry!");
Answer = input.nextLine();
}
}

关于java - 代码仅在一种情况下不断跳过一行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59332469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com