2015-06-05 21 views
0

這是我製作的代碼,它會擲出兩個骰子直到出現一對骰子。 我的問題是,有沒有辦法讓用戶輸入他/她想要的任何數量的骰子?如何讓用戶從控制檯輸入任意數量的變量

我不想創建50個int骰子。如果我使用數組或列表,我會遇到同樣的問題。我必須將每個數組部分分配給numbergen 50次或更多次。也許有我失蹤的東西?

static void Main(string[] args) 
     { 

      Random numbergen = new Random(); 
      int dice1=0; 
      int dice2=1; 
      for (int counter = 0; counter <= 1; counter++) 
      { 
       while (dice1 != dice2) 
       { 
        dice1 = numbergen.Next(1, 7); 
        dice2 = numbergen.Next(1, 7); 
        if (dice1 == dice2) 
        { 
         Console.ForegroundColor = ConsoleColor.Yellow; 
         Console.WriteLine(dice1 + "\t" + dice2); 
         counter++; 
         dice1 = 0; 
         dice2 = 1; 


        } 
        else if (dice1 != dice2) 
        { 
         Console.ForegroundColor = ConsoleColor.White; 
         Console.WriteLine(dice1 + "\t" + dice2); 
        } 
        if (counter ==1) 
        { 
         break; 
        } 
       } 
       } 
      Console.ReadLine(); 

     } 
+2

從它聽起來像你的標題需要一個'列表'或'INT []'但我不知道你實際上是在尋找。 – Habib

+2

如果有七個或更多的骰子,那麼答案總是有一對。你不能有一個到六個之間的七個數字,並且它們都是不同的。 –

+0

呃我不知道如何列表會幫助。如果用戶想滾動20個骰子,我將不得不從0-20列出每個@Habib –

回答

0

以下是所有死亡必須匹配的版本。

using System; 

namespace Dicey 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int numberOfDice; 

      // check for and retrieve the number of dice the user wants. 
      if (args.Length != 1 || !int.TryParse(args[0], out numberOfDice)) 
      { 
       Console.WriteLine("Please provide the number of dice."); 
       return; // exit because arg not provided 
      } 

      // Must have at least one and set some arbitrary upper limit 
      if (numberOfDice < 1 || numberOfDice > 50) 
      { 
       Console.WriteLine("Please provide a number of dice between 1 and 50"); 
       return; // exist because not in valid range 
      } 

      var dice = new int[numberOfDice]; // create array of die (ints) 
      var rand = new Random(); 
      var match = false; // start with false (match not found yet) 

      while (!match) // loop until match becomes true 
      { 
       var message = string.Empty; 
       match = true; // assume match until something doesn't match 

       for (var i = 0; i < numberOfDice; i++) 
       { 
        // initialize dice at index i with the random number 
        dice[i] = rand.Next(1, 7); 

        // build the message line to write to the console 
        message += dice[i]; // first add the die's number 

        if (i < numberOfDice - 1) 
         message += "\t"; // if not at the end, add a tab 

        // check if the previous die (except for the first of course) has a different number 
        if (i > 0 && dice[i - 1] != dice[i]) 
         match = false; // uh oh, not all match. we have to keep going 
       } 

       // print out the message 
       Console.ForegroundColor = match ? ConsoleColor.Yellow : ConsoleColor.White; 
       Console.WriteLine(message); 
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+0

對我來說這段代碼看起來很複雜,但生病的時候儘量去理解它。在這種情況下,許多其他人建議我使用一個數組或者某種類型,對我來說這似乎是一個死衚衕,我嘗試了幾次去做他們所說的但沒有用的東西。你認爲使用列表和數組可以像他們說的那樣解決這個問題嗎?或者他們不瞭解我的問題,因爲我說得這麼差? @ James R. –

+0

@aimepie我希望我能以你想要的方式實現它,因爲所有的死亡都必須匹配。是的,當你有可變數量的項目需要處理時,數組或列表是最好的東西。如果你把它分解成它的組件,它真的不那麼複雜。祝你好運! –

+0

@aimepie,我添加了一些評論,希望能幫助你理解我的代碼。 –

0

如果你想存儲一些由用戶指定的整數,做那麼最簡單的方式,將可以使用像這樣int x[z]一個數組,其中z是或由用戶指定的號碼,甚至更好,以List<int>的形式創建一個整數列表,您可以根據用戶輸入的編號添加整數。

你不會有同樣的問題,因爲你可以讓一個for循環遍歷你的列表或數組,給它們一個由numbergen賦值的值。

0

試試這個, 要求用戶輸入一些骰子存儲在一個變量中,然後創建一個這樣大小的數組。

相關問題