2015-11-18 127 views
0

我想從同一個函數返回兩個不同的值,但事實證明,您只能返回1,但其他人也可以使用元組和外部參數......我不想不理解如何構造它們,語法很混亂。我嘗試過多次嘗試,通過反覆試驗,但每次程序甚至沒有開始。如果你能向我展示一個非常感謝的解決方案。我設法返回字符串[]卡,但現在我需要了解我怎麼可以從相同的功能來自同一函數的多個不同的返回類型

namespace Testing2._0 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string[] cards = ShuffleCardSystem(); 
      Dealing(cards); 
      Format(); 
      Console.ReadLine(); 

     } 
     static void Format() 
     { 
      //Not important 
     } 


     } 
     static string [] ShuffleCardSystem() 
     { 
      string[] ranks = new string[13] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; 
      int rankCounter = 0; 
      string []suits = new string[4] { "♠", "♣", "♦", "♥" }; 
      int suitsCounter = 0; 
      int shuffle; string temp; 
      Random rnd = new Random(); 
      int[] value = new int[52]; 
      string numbers = string.Empty; 
      int convert; 
      string[]cards = new string[52]; 
      for (int i = 0; i < 52; i++) 
      { 
       cards[i] = ranks[rankCounter] + suits[suitsCounter]; 
       rankCounter++; 
       if (rankCounter == 13) 
       { 
        rankCounter = 0; 
        suitsCounter += 1; 
       } 
      } 
      for (int i = 51; i >= 0; i--) 
      { 
        numbers= string.Empty; 
       shuffle = rnd.Next(0, i); 
       temp = cards[shuffle]; 
       cards[shuffle] = cards[i]; 
       cards[i] = temp; 
       for (int position =0; position<cards[i].Length; position++) 
       { 
        if (char.IsDigit(cards[i][position])) 
        { 
         numbers+=cards[i][position]; 
         convert= Convert.ToInt32(numbers); 
         value[i]= convert; 
         if (value[i] ==1) 
         { 
          value [i]=11; 
         } 

        } 
        if (cards[i].Any(char.IsLetter)) 
        { 
         value[i] = 10; 
        } 
       } 
      } 
      } 

      return cards; // I want to return value 

     } 
     static void Dealing(string[] cards) 
     { 
      //other unrelated code... 
       } 

      } 
     } 
    } 
} 
+2

你爲什麼不回班? –

+0

嘗試返回對象數組。 –

回答

0
public class Shuffled 
{ 
public string[]cards{get;set;} 
public int[] values{get;set;} 
} 

C#是一種面向對象的語言返回INT []值。返回包含來自函數的數據的對象是很常見的。

static Shuffled ShuffleCardSystem() 
+0

對不起,不太瞭解這個概念,所以我創建了一個叫做shuffled的新類,然後將「string [] ShuffleCardSystem()」改爲「static Shuffled ShuffleCardSystem()」?如果是這種情況,那麼我怎麼處理我的Main函數中的字符串[]卡聲明? @ AD.Net –