c# - 让我的掷骰子产生更多的掷骰子?

标签 c#

我正在完成一项任务,我必须在其中制作一个可以扔 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/

相关文章:

c# - 无法使用 nhibernate 插入到表中

c# - 将字符串数组与删除和拆分函数组合到一行代码中?

javascript - 使用 Cordova 的 Windows 8 native 插件

c# - 延迟触发 Lightswitch 自动完成过滤器

c# - 如何通过正则表达式删除字符串中多余的回车和空格?

c# - 在 select 语句中声明显式类型而不是 var

c# - 如何加载测试 WinForms 应用程序?

c# - 使用XPath读取xml

c# - 为什么在匿名方法中不允许使用 out 参数?

c# - Entity Framework 核心一对多,主表上没有外键