首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从带字母和数字的字符串转换成c#如何读取、分离和放入数组

从带字母和数字的字符串转换成c#如何读取、分离和放入数组
EN

Stack Overflow用户
提问于 2015-08-29 22:00:51
回答 3查看 96关注 0票数 0

我需要写一个程序,从一个用户确定的文件,我需要找出哪个扑克手类型是输入到程序中。我有一个地方,我可以在手中计算每一套花色的数量,但我想不出一种方法来找到纸牌的数量,它可以是两位数。如果它在数组中,那会更好,但我现在专注于将它放入字符串中。我主要是想计算每一张牌的值,这样我就可以得到每一手的if条件语句,以找到输入的手与假设的手相匹配。

每个手中的卡片的布局类似于以下文件标题: Flush.txt文件内容:C6C9C10C11C12

这就是我现在拥有的所有程序代码

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace pokertask
{
    class Program
    {
        static void Main(string[] args)
        {

            string fileName = "";
            string fileContent = "";
            int clubSuit = 0;
            int heartSuit = 0;
            int spadeSuit = 0;
            int diamondSuit = 0;
            //int[] array = new int[5];
            string cardNum = "";
            //read text file
            Console.WriteLine("type the file name of the hand that you are    testing including its file extension.");
            fileName = Console.ReadLine();
            try
            {//open and read selected file
                StreamReader inFile = new StreamReader(fileName);
                fileContent = inFile.ReadLine();
                inFile.Close();
                Console.WriteLine("{0}", fileContent);
                Console.ReadLine();
            }
            catch
            {//response if failed
                Console.WriteLine("File Handling error has occured. Press any button to close the program.");
                Console.ReadLine();
            }

            //display 
            foreach (char C in fileContent)
            {
                if ( C == 'C')
                {
                    clubSuit ++;
                }
            }

            foreach (char S in fileContent)
            {
                if (S == 'S')
                {
                    spadeSuit++;
                }

            }

            foreach (char H in fileContent)
            {
                if (H == 'H')
                {
                    heartSuit++;
                }
            }

            foreach (char D in fileContent)
            {
                if (D == 'D')
                {
                    diamondSuit++;
                }

            }
            Console.WriteLine(" number of clubs {0}", clubSuit);
            Console.WriteLine(" number of spades {0}", spadeSuit);
            Console.WriteLine(" number of hearts {0}", heartSuit);
            Console.WriteLine(" number of diamonds {0}", diamondSuit);
            Console.ReadLine();


            cardNum = fileContent.Trim('C','H','D','S');
            Console.WriteLine("{0}", cardNum);
            Console.ReadLine();
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2015-08-29 22:16:40

尝尝这个

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "C 6 C 9 C 10 C 11 C 12";
            string[] array = input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < 10; i += 2)
            {
                string suit = array[i];
                string rank = array[i + 1];

                Console.WriteLine("Card {0} , Suit : {1}, Rank : {2}" , i/2, suit, rank);
            }
            Console.ReadLine();
        }
    }
}
​
票数 0
EN

Stack Overflow用户

发布于 2015-08-29 22:16:46

正如其他人已经建议的那样,最好使用更灵活的格式,如XML和JSON,但如果您坚持使用这种格式:

您可以按空格拆分输入,并以两个一组的方式读取数据:

代码语言:javascript
复制
var split = fileContent.Split(' ');
var cards = new List<KeyValuePair<string, int>>();
for (var i = 0; i < split.Length; i += 2)
{
    var suit = split[i];
    var digits = int.Parse(split[i + 1]);
    cards.Add(new KeyValuePair<string, int>(suit, digits));
}
// TODO: read the input from cards list

这里定义的纸牌列表由KeyValuePairs组成,其中关键是花色(例如"C"),值是数字(例如9)

票数 0
EN

Stack Overflow用户

发布于 2015-08-29 22:18:41

首先,我建议您使用容器来存放纸牌套装。List<int>就可以了,但您可能需要更多,这取决于您的需要(Dictclass?)。我不认为这个解决方案是理想的,而且没有错误检查,但它采用了您在示例文件中显示的布局。

代码语言:javascript
复制
var clubs = new List<int>();
var hearts = new List<int>();
var diamonds = new List<int>();
var spades = new List<int>();

然后使用与下面显示的代码片段类似的代码来解析您的套装。

代码语言:javascript
复制
var parts = fileContent.Split(' ');

for (int i = 0; i < parts.Length; i+=2)
{
    if (parts[i] == "C")
    {
        clubs.Add(Int32.Parse(parts[i+1]));
    }
}

完整程序如下:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Poker
{
    class Program
    {

        static void Main(string[] args)
        {
            var clubs = new List<int>();
            var hearts = new List<int>();
            var diamonds = new List<int>();
            var spades = new List<int>();

            var fileContent = "C 6 C 9 C 10 C 11 C 12";

            var parts = fileContent.Split(' ');

            for (int i = 0; i < parts.Length; i += 2)
            {
                switch (parts[i])
                {
                    case "C": clubs.Add(Int32.Parse(parts[i + 1])); break;
                    case "H": hearts.Add(Int32.Parse(parts[i + 1])); break;
                    case "D": diamonds.Add(Int32.Parse(parts[i + 1])); break;
                    case "S": spades.Add(Int32.Parse(parts[i + 1])); break;
                    default:
                        break;
                }
            }

            Console.WriteLine("Clubs: {0}", string.Join(" ", clubs));
            Console.WriteLine("Hearts: {0}", string.Join(" ", hearts));
            Console.WriteLine("Diamonds: {0}", string.Join(" ", diamonds));
            Console.WriteLine("Spades: {0}", string.Join(" ", spades));

            Console.ReadLine();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32286854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档