我正在完成一项任务,我必须在其中制作一个可以扔 1-4 个骰子的应用程序。然后将结果添加到列表中。
然而 ,如果其中任何一个是 6 ,那个没有添加到列表中,而是 掷出 2 个额外的骰子 .
只要骰子返回 6 , 必须再掷 2 个骰子,直到没有骰子返回 6。
这里有人知道如何解决这个问题吗?我的编程技能非常基础,从去年开始我就很少使用它们了。
for (int i = 0; i < qty; i++)
{
int diceNr = RollDice(random);
dicelist.Add(diceNr);
Console.WriteLine(diceNr);
if (diceNr == 6)
{
dicelist.Remove(diceNr);
Console.WriteLine("You got a six, that means you get 2 extra throws!");
for (int x = 0; x < 2; x++)
{
diceNr = RollDice(random);
dicelist.Add(diceNr);
}
最佳答案
您可以尝试使用 while
循环如下:
int i = quantity; #initialize a variable to your maximum number of throws
while(i > 0){ #until you have throws yet
i--; #this is the equivalent of one throw
int diceNr = RollDice(random);
dicelist.Add(diceNr);
Console.WriteLine(diceNr);
if(diceNr == 6){
Console.WriteLine("You got a six, that means you get 2 extra throws!");
i = i + 2; #add your bonus throws
}
}
关于c# - 让我的掷骰子产生更多的掷骰子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61725795/