2017-10-28 6 views
-2

こんにちは、私は非常にC#とコーディングに新しいので、いくつかの基本的な助けが必要です。 ユーザーが複数のダイス(2,3,4,5,6,7,8など)をロールすることを選択した場合、すべてのダイスでランダムにロールするにはどうしますか?たとえば、「Dices rolled:2,5,3」となります。代わりに、現在は "Dices rolled:2,2,2"、または "4,4,4"、basiacllyは同じ数字です。基本ダイス番号ジェネレータをロールバック

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

サイドノート:「dices」という用語が正しくない。複数形は「サイコロ」です。単数形は「死ぬ」。 –

+0

投稿する前にGoogleと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

ありがとうございます 私はいつもこれらの小さなものに悩まされています。 –

関連する問題