C#控制台--字符串拆分/连接
我已经编写了一个基本的控制台应用程序,但它在我标记的几个地方给了我错误。这是我第一次尝试拆分字符串之类的东西,我不知道如何使用构造函数吐出随机的“手”数组C#控制台--字符串拆分/连接,c#,C#,我已经编写了一个基本的控制台应用程序,但它在我标记的几个地方给了我错误。这是我第一次尝试拆分字符串之类的东西,我不知道如何使用构造函数吐出随机的“手”数组 namespace PokerGame { public class Display { static void Main() { Console.WriteLine("Welcome to the Poker Game!"); Console.WriteLine("\t" + "Enter '
namespace PokerGame
{
public class Display
{
static void Main()
{
Console.WriteLine("Welcome to the Poker Game!");
Console.WriteLine("\t" + "Enter 'deal' to start game!");
Console.WriteLine("Hand is: {0}");
}
}
public class NewDeal
{
public string card;
public string[] suit = {"♥,♠,♦,♣"};
public char delimiter = ',';
public string[] rank = {"A,1,2,3,4,5,6,7,8,9,10,J,Q,K"};
public string dealer(string suit, string rank)
{
string[] singleSuit = suit.Split(delimiter);
string[] singleRank = rank.Split(delimiter);
foreach(string s in singleSuit)
{
string card = s + singleRank;
}
return card;
}
public NewDeal()
{
}
}
}名称空间扑克游戏
{
公开课展示
{
静态void Main()
{
Console.WriteLine(“欢迎来到扑克游戏!”)
}您的代码有多处错误。例如,
dealer
参数将隐藏类变量。您还尝试将分隔符作为一个数组使用,而它是一个字符。您还必须决定您的方法是否真的是静态的
,因为您的变量不是静态的e还在您的foreach
循环中返回card
,阻止它运行……这里有很多问题,所以很难提供帮助。我现在更了解这一点,但我的问题是我无法返回“card”使用此方法会给我一个错误。我还没有尝试过你的。当我尝试返回“card”时,它表示它与以前的声明冲突…为什么你不将参数传递给dealer()?让我知道你的程序的逻辑,你希望它做什么。用户是否想输入一些值或什么,因为你问了,但你没有检索到任何东西是的,我还没有为用户输入逻辑,但基本上用户输入了一些东西,系统打印出一张“手”,这是一组“卡”。每张卡都是一套西装和等级。就这些……好吧,我按照自己的方式修改了程序,重点是电脑随机选择了4张卡,我希望这就是你想要的
Console.WriteLine("Pick a card?(\"no\" to exit): ");
string sentinelValue = Console.ReadLine();
NewDeal oNewDeal = new NewDeal();
Random newCard = new Random(System.DateTime.Now.Second);
string _newCard = "";
while (sentinelValue != "no")
{
_newCard = oNewDeal.getCards(newCard.Next(0, oNewDeal.suit.Length - 1), newCard.Next(0, oNewDeal.rank.Length - 1));
Console.Write("Card: {0}\n", _newCard);
Console.WriteLine("Pick a card?(\"no\" to exit): ");
sentinelValue = Console.ReadLine();
}
Console.Read();
}
}
public class NewDeal
{
public string card = "";
public string[] suit = new string[] { "♥","♠","♦","♣" };
public const char DELIMETER = ',';
public string[] rank = new string[] {"A","1","2","3","4","5","6","7","8","9","10","J","Q","K"};
public string getCards(int _suit, int _rank)
{
return card = rank[_rank] + suit[_suit];
}
}