简体   繁体   English

c#从带有字母和数字的字符串开始,如何读取和分隔并放置在数组中

[英]c# from string with letters and numbers how to read and seperate into and place in array

i need to write a program where from a user determined file i need to find which poker hand type is input into the program. 我需要编写一个程序,在该程序中,需要从用户确定的文件中查找输入哪种扑克手类型的程序。 i have where i can count the number of each suit in the hand but i cant think of way to find the numbers of the cards in a way it can have double digits. 我在哪里可以算出手中每套西装的数量,但我想不出以双位数来查找卡片数量的方法。 it would be preferable if it was in an array but am focusing on getting it into a string for now. 如果它位于数组中,但现在重点是将其放入字符串中,则会更好。 im mostly wanting to count each card value so then i can have if statements of the criteria for each hand to find the inputed hand matches what the hand is supposed to be 我主要想计算每张卡的价值,这样我就可以确定每只手的标准陈述以找到输入的手是否与该手应该是的相符

the layout for the cards in each hand are like the following file title: Flush.txt file content: C 6 C 9 C 10 C 11 C 12 每手牌的布局类似于以下文件标题:Flush.txt文件内容:C 6 C 9 C 10 C 11 C 12

this is all the code i have for the program right now 这就是我现在程序的所有代码

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();
        }
    }
}

Try this 尝试这个

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();
        }
    }
}
​

As already suggested by others, it is much better to use a more flexible format like XML and JSON, but if you are stuck with this format: 正如其他人已经建议的那样,最好使用更灵活的格式,例如XML和JSON,但是如果您坚持使用这种格式:

You could split the input by spaces and read the data in groups of two: 您可以按空格分隔输入,并以两个为一组读取数据:

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

The cards list defined up there is made up of KeyValuePairs where the Key is the suit (eg "C") and Value is the digit (eg 9) 在那里定义的牌列表由KeyValuePairs组成,其中Key是西服(例如“ C”),Value是数字(例如9)

First I would recommend using a container for your card suits. 首先,我建议您为您的西装套装使用一个容器。 List<int> will do, but you may need something more depending on your needs ( Dict ? class ?). List<int>可以,但是根据您的需要,您可能还需要其他内容( Dictclass ?)。 I don't think this solution is ideal and there's no error checking, but it assumes the layout you show in your example file. 我认为此解决方案不是理想的选择,也没有错误检查,但是它假定您在示例文件中显示的布局。

var clubs = new List<int>();
var hearts = new List<int>();
var diamonds = new List<int>();
var spades = new List<int>();

Then use a code similar to the snippet shown below to parse your suits. 然后使用类似于下面显示的代码段的代码分析西装。

var parts = fileContent.Split(' ');

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

Full program below: 完整程序如下:

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM