我可以阻止用户在循环中选择相同的 switch case 两次吗?
我必须使用扫描仪制作骰子游戏。 我将有 3 个骰子,用户将被要求选择扔哪一个。 用户不能一次扔掉所有的东西——只能一个接一个地挑选。 在一轮 3 次投掷中不允许使用同一个骰子两次,但用户可以按照自己想要的顺序拿走骰子。 比赛的总比分即将达到12分。 我尝试过使用 if 语句和 switch - 但我还没有找到解决方案,让用户不能两次接受相同的模具。 我删除了一些代码,但我用 switch 再次尝试。
我的代码是:
// 声明变量
int diceOne = 0;
int diceTwo = 0;
int diceThree = 0;
int rolls;
int rollOne;
int rollTwo;
int rollThree;
int rounds=1;
int sumOfDice =0;
String welcome = "[ Welcome to the game 12! Try to get the total sum of 12 by rolling 1-3 dices ]";
String userInfo = "Enter which dice you want to roll [1,2,3] (Exit game with q): ";
// Create Scanner to obtain data input from user.
Scanner twelf = new Scanner(System.in);
// Welcome message
System.out.print("\n" + welcome + "\n");
// while loop
while(rounds<=3){
System.out.print("\n"+userInfo + " ");
rolls = twelf.nextInt();
rounds= rounds + 1;
rolls =+ rolls;
switch(rolls){
case 1:
diceOne = (int)(Math.random()*6)+1;
System.out.print("Dice 1 hits " + diceOne);
break;
case 2:
diceTwo = (int)(Math.random()*6)+1;
System.out.print("Dice 2 hits " + diceTwo);
break;
case 3:
diceThree = (int)(Math.random()*6)+1;
System.out.print("Dice 3 hits " + diceThree);
break;
default:
}
I have to make a dice game using Scanner.
I will have 3 dice and the user will be asked to choose which one to throw.
The user can't throw all at once - just pick one after another.
It's not allowed to use the same die twice in one round of 3 throws, but user can take the dices in the order user wants.
The game is about to hit a sum of 12.
I have tried to use if statements and switch - but I haven't found a solution for that user can't take same die twice.
I erased some code, but I tried again with switch.
My code is:
// Declaring variables
int diceOne = 0;
int diceTwo = 0;
int diceThree = 0;
int rolls;
int rollOne;
int rollTwo;
int rollThree;
int rounds=1;
int sumOfDice =0;
String welcome = "[ Welcome to the game 12! Try to get the total sum of 12 by rolling 1-3 dices ]";
String userInfo = "Enter which dice you want to roll [1,2,3] (Exit game with q): ";
// Create Scanner to obtain data input from user.
Scanner twelf = new Scanner(System.in);
// Welcome message
System.out.print("\n" + welcome + "\n");
// while loop
while(rounds<=3){
System.out.print("\n"+userInfo + " ");
rolls = twelf.nextInt();
rounds= rounds + 1;
rolls =+ rolls;
switch(rolls){
case 1:
diceOne = (int)(Math.random()*6)+1;
System.out.print("Dice 1 hits " + diceOne);
break;
case 2:
diceTwo = (int)(Math.random()*6)+1;
System.out.print("Dice 2 hits " + diceTwo);
break;
case 3:
diceThree = (int)(Math.random()*6)+1;
System.out.print("Dice 3 hits " + diceThree);
break;
default:
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
程序到底要求什么以及输出应该是什么仍然有点模糊。但似乎您可能需要一个条件语句来检查某些骰子是否已经掷出。
It's still a little vague what exactly the program is asking and what the output should be. But it seems that you might need a conditional statement that checks if that certain dice had been rolled already.
您可能希望使用映射来跟踪骰子是否掷出,而不是使用 switch 语句。
rolledDiceMap
将存储您的三个骰子,它有一个Integer
键和一个Dice
值。为此,我创建了一个
Dice
类,它有两个字段:value
:滚动的随机值;rolled
:布尔标志,如果为 true,则重复 while 循环,直到用户输入合适的值并进入下一轮。我尝试在下面的代码中添加一些希望有用的注释:
编辑注释中描述的方法,而不使用地图:
Instead of using switch statements, you may want to use a map to track if dice were rolled or not.
rolledDiceMap
will store your three dice, it has anInteger
key and aDice
value.For this I created a
Dice
class that has two fields:value
: the random value that gets rolled);rolled
: boolean flag, if this is true repeat the while loop until the user enters a good value and moves to the next round.I tried to add some, hopefully, useful comments to the code below:
Edit for the approach described in comments, without using map: