gpt4 book ai didi

java - 当用户输入错误的输入时,我不知道如何重新启动循环

转载 作者:行者123 更新时间:2023-12-01 18:35:30 25 4
gpt4 key购买 nike

好吧,对于一项作业,我需要创建一个循环,让用户输入数字 1-4 来表示剪刀石头布或结束游戏,但是如果他们输入了除这些以外的任何内容,我需要他们能够重新输入数字,但我似乎无法让我的循环重新启动或类似的事情......

代码(这只是我的 while 循环

while(user < 5)
{
System.out.println("Please enter in a number");
computer = generator.nextInt(3) + 1;
user = keyboardIn.nextInt();

//tell the player what was chosen
if(user == 1)//player is rock
{
System.out.println ("Player is rock");
}
else if (user == 2)//player is paper
{
System.out.println ("Player is paper");
}
else if (user == 3) //player is scissors
{
System.out.println ("Player is scissors");

}
else if (user == 4)
{
System.out.println("Thank you for playing!");
break;
}
else if (user >= 5)
{
System.out.println(user + " was never an option. \nTry again.");
user = keyboardIn.nextInt();
}

//tell the player what the computer has chosen
if(computer == 1)//computer is rock
{
System.out.println ("Computer is rock");
}
else if (computer == 2)//computer is paper
{
System.out.println ("Computer is paper");
}
else if (computer == 3)//computer is scissors
{
System.out.println ("Computer is scissors");
}


//determine winner
if (user == computer) //tie
{
System.out.println("It is a tie");
}
else if (user < computer) //player is rock
{
System.out.println("Computer wins");
}
else //computer is scissors
{
System.out.println("Player wins");
}
System.out.println();
}

最佳答案

您可以使用继续跳回循环顶部。但请注意,在顶部您会要求用户输入,并且在出错时您也会这样做;这一次太多了。

关于java - 当用户输入错误的输入时,我不知道如何重新启动循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60060892/

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