我是C#中的結構新手,我被卡住了,無法將文件中的值傳遞迴結構並再次成功讀取它們。結構數組| |沒有傳遞值
初始化值正常工作,但該文件將不會在所有
加載這是我到目前爲止有:
public struct Card
{
public char suit;//'C'- Clubs, 'D'-Diamonds,'H'- Hearts
//and 'S'- Spades
public int value;//2-14 – for 2-10, Jack, Queen, King, Ace
}
static void Main(string[] args)
{
const int CARDS_IN_HAND = 5;//number of cards in hand
Card[] hand = new Card[CARDS_IN_HAND];//array of cards
InitialiseArray(hand);
Console.WriteLine("-------------\n");
Console.WriteLine("Initialised Values in Array");
DisplayHandData(hand);
Console.ReadLine();
這個問題似乎低於
LoadArray(hand);//load values into array for use
//check values loaded to array
Console.WriteLine("-------------\n");
Console.WriteLine("Actuals Values in Array from file");
DisplayHandData(hand);
Console.ReadLine();
}
public static void InitialiseArray(Card[] data)
{
for (int count = 0; count < data.Length; count++)
{
data[count].suit = 'C';
data[count].value = 0;
}
}
public static void LoadArray(Card[] data)
{
string fileName = "FullHouse.txt";//name of file for menu item 3
//data in file = C 13 H 13 D 13 C 10 H 10
string input = fileName;
List<Card> cards = new List<Card>();
//string input = fileName;
StreamReader inFile = new StreamReader(fileName);//open file
input = inFile.ReadLine();//priming read for array
string[] inputArray = input.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
//input loading data into array
for (int i = 0; i < 10; i += 2)
{
Card newCard = new Card();
newCard.suit = inputArray[i][0];
newCard.value = int.Parse(inputArray[i + 1]);
cards.Add(newCard);
}
inFile.Close();
}
public static void DisplayHandData(Card[] data)
{
Console.WriteLine();
//test values loaded into array in correct positions
for (int records = 0; records < data.Length; records++)
{
Console.WriteLine(data[records].suit);
Console.WriteLine(data[records].value);
}
Console.WriteLine("-------------\n");
Console.ReadLine();
}
任何幫助將不勝感激。
你似乎是從C背景。不要使用char類型的卡片,請考慮使用枚舉。 –
你有沒有關於這將如何看待的例子? – Coop1979
你可以閱讀關於枚舉:https://msdn.microsoft.com/en-us/library/sbbt4032.aspx?f=255&MSPPError=-2147217396 –