2017-10-28 74 views
-2

你好我对C#和编码很新,所以需要一些基本的帮助。 如果用户选择滚动多个骰子(2,3,4,5,6,7,8等),你会怎么做才能使它在所有骰子上随机滚动?例如:“骰子滚动:2,5,3”。而不是它现在是“掷骰:2,2,2”或“4,4,4”,基本上是相同的数字。基本掷骰子号码发生器

static int RollTheDice(Random rndObject) 
    { 
     Random dice = new Random(); 
     int nr = dice.Next(1, 7); // if user requests to roll multiple dices how 
            // do you make all the rolls random and not the same 


     return nr; 
    } 

    static void Main() 
    { 
     Random rnd = new Random(); 
     List<int> dices = new List<int>(); 

     Console.WriteLine("\n\tWelcome to the dicegenerator!"); 


     bool go = true; 
     while (go) 
     { 
      Console.WriteLine("\n\t[1] Roll the dice\n" + 
       "\t[2] Look what you rolled\n" + 
       "\t[3] Exit"); 
      Console.Write("\tChoose: "); 
      int chose; 
      int.TryParse(Console.ReadLine(), out chose); 

      switch (chose) 
      { 
       case 1: 
        Console.Write("\n\tHow many dices do you want to roll?: "); 
        bool input = int.TryParse(Console.ReadLine(), out int antal); 

        if (input) 
        { 
         for (int i = 0; i < antal; i++) 
         { 
          dices.Add(RollTheDice(rnd)); 
         } 
        } 
        break; 
       case 2: 
        Console.WriteLine("\n\tDices rolled: "); 
        foreach (int dice in dices) 
        { 
         Console.WriteLine("\t" + dice); 
        } 
        break; 
       case 3: 
        Console.WriteLine("\n\tThank you for rolling the dice!"); 
        Thread.Sleep(1000); 
        go = false; 
        break; 
       default: 
        Console.WriteLine("\n\tChoose between 1-3 in the menu."); 
        break; 
+1

附注:术语“骰子”不正确。复数是“骰子”。奇异是“死亡”。 –

+0

在发布之前,Google和Google以及谷歌还有更多。几乎所有的新程序员都需要的答案已经在这里。请阅读[问]并参加[导游] – Plutonix

回答

-1

您正在创建一个新的Random每一次,如果称为短的时间内,这将产生类似的号码。请参阅此处:How do I generate a random int number in C#?

您已经将Random传递给您的函数,请使用它而不是创建一个新函数!

static int RollTheDice(Random rndObject) 
{ 
    int nr = rndObject.Next(1, 7); // if user requests to roll multiple dices how 
           // do you make all the rolls random and not the same 
    return nr; 
} 
+3

您的链接很好。为什么你不投票关闭,而是发布一个新的答案,这并没有说更多 –

+0

谢谢 我总是被困在这些小事情上,我脸上的许多facepalms –