Java 如何从列表中删除选项并显示错误消息以再次提示用户
它应该做什么:如果玩家输入2 3或4,选择它的选项应该在第二轮消失,如果用户尝试使用选择的选项,则会出现错误消息。同样的例子,但是正确的方法Java 如何从列表中删除选项并显示错误消息以再次提示用户,java,arrays,Java,Arrays,它应该做什么:如果玩家输入2 3或4,选择它的选项应该在第二轮消失,如果用户尝试使用选择的选项,则会出现错误消息。同样的例子,但是正确的方法 dice are: 2 2 4 1 to roll a single dice 2 for a yahtzee score 3 for a straight score 4 for a chance score Enter a choice 1 What die would you like to roll1 dice are: 4 2 4 1 to ro
dice are: 2 2 4
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
4 for a chance score
Enter a choice
1
What die would you like to roll1
dice are: 4 2 4
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
4 for a chance score
Enter a choice
4
your current total is: 10
dice are: 5 5 5
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
4 for a chance score
Enter a choice
2
your current total is: 50
dice are: 4 4 1
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
4 for a chance score
Enter a choice
我尝试过使用几种不同的方法来解决这个问题,但这是我目前的方法,如果有人对我的解决方案有想法,我会非常满意 下面是我要做的: 在promptUser方法中,在行之后
dice are: 2 2 4
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
4 for a chance score
Enter a choice
1
What die would you like to roll1
dice are: 4 2 4
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
4 for a chance score
Enter a choice
4
your current total is: 10
dice are: 5 5 5
1 to roll a single dice
2 for a yahtzee score
3 for a straight score
Enter a choice
2
your current total is: 50
dice are: 4 4 1
1 to roll a single dice
3 for a straight score
Enter a choice
4
Error invalid option try again
3
我会检查选项是什么,是2、3还是4。
从这里,如果是2,请致电noMoreYahtzee
或者如果是3,则调用NoMoreStraight;如果是4,则调用noMoreChance
然后,因为您已经在displayChoices方法中编写了检查布尔值的代码,所以您的代码应该可以工作。希望这有帮助
此外,whiletrue循环将导致一个无限循环。
为了解决这个问题,我将在promptUser方法中添加一些东西
int choice = input.nextInt();
那么你是说做这样的事情如果选择==2{noMoreYahtzee;}否则如果选择==3{noMoreStraight;}否则如果选择==4{noMoreChance;}我厌倦了,但发生的是我陷入了whiletrue循环,我不断被提示输入我的选择是的,正是这样。噢,天哪,我甚至没看到whiletrue循环。您陷入困境的原因是,while循环的参数为false时将停止运行。在你的情况下,真实总是真实的。这意味着它会导致无限循环。让我编辑我的原始答案。请给我一点时间,为这个问题提供最少的信息
int choice = input.nextInt();
public int promptUser(){
boolean check = true;
int choice;
while(check){
System.out.println("Enter a choice");
Scanner input = new Scanner(System.in);
choice = input.nextInt();
if( (numberOfRolls < 3 && choice == 1) || (canYahtzee && choice ==2) || (canStraight && choice == 3) || (canChance && choice == 4)){
check = false;
}
}
return choice;
}