[英]c# from string with letters and numbers how to read and seperate into and place in array
我需要編寫一個程序,在該程序中,需要從用戶確定的文件中查找輸入哪種撲克手類型的程序。 我在哪里可以算出手中每套西裝的數量,但我想不出以雙位數來查找卡片數量的方法。 如果它位於數組中,但現在重點是將其放入字符串中,則會更好。 我主要想計算每張卡的價值,這樣我就可以確定每只手的標准陳述以找到輸入的手是否與該手應該是的相符
每手牌的布局類似於以下文件標題:Flush.txt文件內容:C 6 C 9 C 10 C 11 C 12
這就是我現在程序的所有代碼
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();
}
}
}
嘗試這個
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();
}
}
}
正如其他人已經建議的那樣,最好使用更靈活的格式,例如XML和JSON,但是如果您堅持使用這種格式:
您可以按空格分隔輸入,並以兩個為一組讀取數據:
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組成,其中Key是西服(例如“ C”),Value是數字(例如9)
首先,我建議您為您的西裝套裝使用一個容器。 List<int>
可以,但是根據您的需要,您可能還需要其他內容( Dict
? class
?)。 我認為此解決方案不是理想的選擇,也沒有錯誤檢查,但是它假定您在示例文件中顯示的布局。
var clubs = new List<int>();
var hearts = new List<int>();
var diamonds = new List<int>();
var spades = new List<int>();
然后使用類似於下面顯示的代碼段的代碼分析西裝。
var parts = fileContent.Split(' ');
for (int i = 0; i < parts.Length; i+=2)
{
if (parts[i] == "C")
{
clubs.Add(Int32.Parse(parts[i+1]));
}
}
完整程序如下:
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.