我真的无法将任何想法打印到控制台窗口吗?加上对我的代码的任何建议都会受到欢迎。
namespace Drinking_Game_
{
class Program
{
这是我遇到麻烦的地方。我无法将我打电话的卡片打印到ConsoleWindow。
public static void Main()
{
Deck deck = new Deck();
Card card = new Card();
deck.Shuffle();
var value = deck.TakeCard();
Console.WriteLine(value);
Console.ReadLine();
}
}
public enum Suit
{
Spades = 0,
Hearts = 1,
Diamonds = 2,
Clubs = 3,
}
public enum CardNumber
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
}
public class Card
{
public Suit Suit { get; set; }
public CardNumber CardNumber { get; set; }
}
public class Deck
{
public Deck()
{
Reset();
}
public List<Card> Cards { get; set; }
public void Reset()
{
Cards = Enumerable.Range(1, 4)
.SelectMany(s => Enumerable.Range(1, 13)
.Select(c => new Card()
{
Suit = (Suit)s,
CardNumber = (CardNumber)c
}
)
).ToList();
}
public void Shuffle()
{
Cards = Cards.OrderBy(c => Guid.NewGuid()).ToList();
}
public Card TakeCard()
{
var card = Cards.FirstOrDefault();
Cards.Remove(card);
return card;
}
public IEnumerable<Card> TakeCards(int numberOfCards)
{
var cards = Cards.Take(numberOfCards);
var takeCards = cards as Card[] ?? cards.ToArray();
Cards.RemoveAll(takeCards.Contains);
return takeCards;
}
}
}
答案 0 :(得分:0)
您需要覆盖卡类下的tostring。
答案 1 :(得分:0)
您需要覆盖ToString()
隐式继承的Object
类的Card
方法的默认实现。
例如:
public class Card
{
public Suit Suit { get; set; }
public CardNumber CardNumber { get; set; }
public override string ToString()
{
return CardNumber + " of " + Suit;
}
}
此外,您应将Suit
枚举修改为1到4。