我已在WebMatrix中使用RazorC#语法将字符串拆分为单个字母,现在如何用结果填充数组?

问题描述

| 任务:显示手牌(13张牌)。这只手来自于格式为KQT5.KJ873..AJ52的数据库,其中西服顺序为黑桃,红心,钻石球杆,句点用于分隔西服。我希望创建这只手的2D数组,即 [S,K] [S,Q] [S,T] [S,5] [H,K] [H,J] [H,8] [H,7] [H,3] (钻石中有空隙) [C,A] [C,J] [C,5] [C,2] 到目前为止,我在WebMatrix中使用Razor(C#)的代码
@{ string westHand = \"KQT5.KJ873..AJ52\";

                foreach (string subString2 in westHand.Split(\'.\')) {
                    @subString2 <br />

                    foreach (char c in subString2){
                        @c <br />

                }
}}
输出是 KQT5 ķ 问 Ť 5 KJ873 ķ Ĵ 8 7 3 AJ52 一种 Ĵ 5 2 现在将各个卡分开。就像我上面说的,我想把它放到一个二维数组中:
string[,] handData = new string[12,12]
嘿,如果我能算出如何将数字放入一维数组中,我会很高兴。 编辑:如下所述,所需数组的尺寸应为[13,2],即13行乘2列。     

解决方法

        编辑: 好的,我想这正是您要的。最后,手包含一个字符数组:hand [纸牌索引,0-12] [0是花色,1是纸牌]
    char[] suits = { \'S\',\'H\',\'D\',\'C\' };
    char[,] hand = new char[13,2];
    string westHand = \"KQT5.KJ873..AJ52\";
    String output = new String();
    int currentSuit = 0; //Iterator for suits (0-4)
    int currentCard = 0; //Current # of card from hand (0-12)
    foreach (string suitString in westHand.Split(\'.\')) {
        foreach (char cardChar in suitString){
            hand[currentCard,0] = suits[currentSuit];
            hand[currentCard,1] = cardChar;
            currentCard++;
        }
        currentSuit++;
    }
    for(int x = 0; x < 13; x++)
    {
        output += \"[\" + hand[x,0] + \",\" + hand[x,1] + \"]\";
    }
}
输出值:
[S,K][S,Q][S,T][S,5][H,K][H,J][H,8][H,7][H,3][C,A][C,J][C,5][C,2]
上一个答案,以防万一您仍然需要它: 我认为这是您正在尝试做的事情。这只是直接的C#,但是使用类,因为这是一种面向对象的语言。 :)
    char[] suits = { \'S\',\'C\' };
    String output = new String();
    List<Card> hand = new List<Card>();
    string westHand = \"KQT5.KJ873..AJ52\";
    int currentSuit = 0;

    foreach (string suitString in westHand.Split(\'.\')) {
        foreach (char cardChar in suitString){
            Card newCard = new Card(suits[currentSuit],cardChar);
            hand.Add(newCard);
        }
        currentSuit++;
    }

    foreach (Card currentCard in hand)
    {
        output += currentCard.ToString();
    }
这是Card类:
public class Card
{
    public char suit,type;

    public Card(char suit,char type)
    {
        this.suit = suit;
        this.type = type;
    }

    public String ToString()
    {
        return \"[\" + this.suit + \",\" + this.type + \"]\";
    }

}
输出:
[S,2]
同样,我认为这是您想要的,但是我不确定。让我知道我是否离基地远一点。     ,        我不确定是要显示帖子开始时写的卡片,还是要“放入”数组以执行其他操作?但是,仅以所需格式显示它们,以下代码将起作用:
@{ string westHand = \"KQT5.KJ873..AJ52\";

   char type = \'S\'; //start with spades

                foreach (string subString2 in westHand.Split(\'.\')) {

                    foreach (char c in subString2){
                        <text>[@type,@c]</text>
                }
                switch (type)
                {
                    case \'S\': type = \'H\'; break;
                    case \'H\': type = \'D\'; break;
                    case \'D\': type = \'C\'; break;
                }
}}
编辑:如果您真的只想将它们放在具有13行2列的数组中,请使用以下代码。 (变量结果包含具有正确值的数组)
string westHand = \"KQT5.KJ873..AJ52\";

        char type = \'S\'; //start with spades

        string[,] result = new string[westHand.Length - 3,2];

        int counter = 0;
        foreach (string subString2 in westHand.Split(\'.\'))
        {

            foreach (char c in subString2)
            {
                result[counter,0] = type.ToString();
                result[counter,1] = c.ToString();
                counter++;
            }
            switch (type)
            {
                case \'S\': type = \'H\'; break;
                case \'H\': type = \'D\'; break;
                case \'D\': type = \'C\'; break;
            }
        }
    ,        感谢Preli&chrsmtclf。将您的解决方案放入WebMatrix的Razor(C#)语法中,我现在有: (1)Preli的解决方案;和
@{string westHand = \"KQT5.KJ873..AJ52\";
    var num = westHand.Length - 3;        
    char type = \'S\'; //start with spades
    string[,] result = new string[num,2];

    int counter = 0;
    foreach (string subString2 in westHand.Split(\'.\'))
    {
        foreach (char card2 in subString2)
        {
             @: [@type,@card2]
            result[counter,0] = type.ToString();
            result[counter,1] = card2.ToString();
            counter++;
        }
        switch (type)
        {
            case \'S\': type = \'H\'; break;
            case \'H\': type = \'D\'; break;
            case \'D\': type = \'C\'; break;
        }
    }       
}
  <br /> You have @num cards. <br />        
  @for(var i = 0; i < num; i++)  
{ 
   @result[i,0] ; @result[i,1] ;<br /> 
}
(2)chrsmtclf的解决方案
@{  char[] suits = { \'S\',\'C\' };
char[,2];
string westHand = \"KQT5.KJ873..AJ52\";
int currentSuit = 0; //Iterator for suits (0-4)
int currentCard = 0; //Current # of card from hand (0-12)
foreach (string suitString in westHand.Split(\'.\')) {
    foreach (char cardChar in suitString){
        hand[currentCard,0] = suits[currentSuit];
        hand[currentCard,1] = cardChar;
        currentCard++;
    }
    currentSuit++;
}
}

@for(var i = 0; i < 13; i++)  
{ 
  @hand[i,0] ; @hand[i,1] ;<br /> 
}
上面的两个解决方案都提供了二维数组中包含的以下输出: SK 平方 ST S5 香港 HJ H8 H7 H3 认证机构 CJ C5 C2