我是一名新手编写器,为我的班级学习C#,我在练习练习时遇到麻烦,我们必须制作一副卡片并将其处理掉。
我们需要创建一个通过构造函数从另一个类接收参数来创建卡片的类。然后在Tostring方法中使用这些值来设置具有switch语句的那些卡的值和适合并返回一张卡。在第二个类中,我们使用卡填充数组,并在Dealing方法中调用Tostring方法,通过将它们拉出数组来随机生成用户指定数量的卡。
问题是我的数组没有被填充我已经尝试将Console.WriteLine部分直接分配给它们并且它们是空的。你不必给我完整的答案,只是让我走上正轨。
以下是代码:
这是卡创建类
` class Card
{
static int value;
static int suit;
static string cdvalue;
static string cdsuit;
string card;
public Card(int ranvalue, int ransuit) //constructor that sets value and suit
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
switch (value) //switch statement for card value
{
case 1: cdvalue = "ace";
break;
case 2: cdvalue = "two";
break;
case 3: cdvalue = "three";
break;
case 4: cdvalue = "four";
break;
case 5: cdvalue = "five";
break;
case 6: cdvalue = "six";
break;
case 7: cdvalue = "seven";
break;
case 8: cdvalue = "eight";
break;
case 9: cdvalue = "nine";
break;
case 10: cdvalue = "ten";
break;
case 11: cdvalue = "jack";
break;
case 12: cdvalue = "queen";
break;
case 13: cdvalue = "king";
break;
}
switch (suit) // switch for card suit
{
case 1: cdsuit = "Hearts ";
break;
case 2: cdsuit = "Spades ";
break;
case 3: cdsuit = "Diamonds ";
break;
case 4: cdsuit = "Clubs ";
break;
}
card = cdvalue + " of " + cdsuit;
return card;// returns a string in the form of "value of suit"`
此课程创建套牌
class Deck
{
Random rng = new Random(); //Random object
private static Card[] deck;
static string[] cards;
public Deck()
{
deck = new Card[52];//creates array of 52 elements
int l = 0;
for (int i = 1; i <= 13; i++)//loops to create cards
{
for (int j = 1; j <= 4; j++)
{
deck[l++] = new Card(i,j); // populates the array with all 52 cards
}
}
}
static string dealt;
static int Rndm;
public string deal(int number)//parameter received from user
{
cards = new string[number];//creates an array to contain dealt card objects
int m = 0;
for (int num=0;num<number;num++) // determines the amount of cards to be dealt
{
Rndm = rng.Next(0,53);
cards[m++] = deck[Rndm].ToString();//fills the card array with randomly dealt cards from the deck
dealt = string.Join(" ", cards); // turns the card array into a single string of the cards dealt
}
return dealt;//returns the cards dealt
这是测试类
static void Main(string[] args)
{
// get the number of cards from the user - must be between 1 and 52
int cardsDealt = -1;
do
{
Console.Write("Enter number of cards to get (1-52): ");
String dealStr = Console.ReadLine();
Boolean parsed = int.TryParse(dealStr, out cardsDealt);
} while (cardsDealt < 1 || cardsDealt > 52);
// create a Deck object
Deck cardDeck = new Deck();
// Call the deal method
String cards = cardDeck.deal(cardsDealt);
// List the result
Console.WriteLine("\nCards dealt:\n" + cards);
答案 0 :(得分:1)
在您的Card类中,您将变量value
和suit
标记为static
。
private static int value;
private static int suit;
由于这两个变量是静态的,因此将对Card对象的实例维护对这些变量的引用。这意味着每次创建新的Card对象时,都会无意中更新具有相同值的所有其他Card对象value
和suit
变量。所以,现在每当你得到一张卡片的实例时,它将与所有其他实例(最后一张卡片)相同。
从这些声明中删除static关键字,你应该没问题。
另外,我建议您阅读静电以更熟悉其用法:Static keyword in c#
答案 1 :(得分:0)
您的代码非常接近正确。您似乎对何时使用static
有误解。
如果您需要在类的所有实例之间共享的变量,请使用static
。但是,如果您需要每个实例值,那么请不要。
您的代码中没有应该是static
的变量。
另外,为了帮助您完成代码,您不需要在类级别定义所有变量。在每种方法中都可以(并且应该)声明很多。
这是您的代码的重构版本,以便您可以看到应该做什么。
void Main()
{
// get the number of cards from the user - must be between 1 and 52
int cardsDealt = -1;
while (cardsDealt < 1 || cardsDealt > 52)
{
Console.Write("Enter number of cards to get (1-52): ");
String dealStr = Console.ReadLine();
Boolean parsed = int.TryParse(dealStr, out cardsDealt);
}
// create a Deck object
Deck cardDeck = new Deck();
// Call the deal method
String cards = cardDeck.deal(cardsDealt);
// List the result
Console.WriteLine("\nCards dealt:\n" + cards);
}
public class Card
{
private int value;
private int suit;
public Card(int ranvalue, int ransuit) //constructor that sets value and suit
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
string cdvalue = null;
switch (value) //switch statement for card value
{
case 1:
cdvalue = "ace";
break;
case 2:
cdvalue = "two";
break;
case 3:
cdvalue = "three";
break;
case 4:
cdvalue = "four";
break;
case 5:
cdvalue = "five";
break;
case 6:
cdvalue = "six";
break;
case 7:
cdvalue = "seven";
break;
case 8:
cdvalue = "eight";
break;
case 9:
cdvalue = "nine";
break;
case 10:
cdvalue = "ten";
break;
case 11:
cdvalue = "jack";
break;
case 12:
cdvalue = "queen";
break;
case 13:
cdvalue = "king";
break;
}
string cdsuit = null;
switch (suit) // switch for card suit
{
case 1:
cdsuit = "Hearts ";
break;
case 2:
cdsuit = "Spades ";
break;
case 3:
cdsuit = "Diamonds ";
break;
case 4:
cdsuit = "Clubs ";
break;
}
string card = cdvalue + " of " + cdsuit;
return card;// returns a string in the form of "value of suit"`
}
}
public class Deck
{
private Random rng = new Random(); //Random object
private Card[] deck;
public Deck()
{
deck = new Card[52];//creates array of 52 elements
int l = 0;
for (int i = 1; i <= 13; i++)//loops to create cards
{
for (int j = 1; j <= 4; j++)
{
deck[l++] = new Card(i, j); // populates the array with all 52 cards
}
}
}
public string deal(int number)//parameter received from user
{
string[] cards = new string[number];//creates an array to contain dealt card objects
for (int num = 0; num < number; num++) // determines the amount of cards to be dealt
{
int Rndm = rng.Next(deck.Length);
cards[num] = deck[Rndm].ToString();//fills the card array with randomly dealt cards from the deck
}
string dealt = string.Join(" ", cards); // turns the card array into a single string of the cards dealt
return dealt;//returns the cards dealt
}
}
唯一的另一个问题是您的交易代码会产生重复的卡片。你可能需要做点什么。
如果我正在编码,我就是这样做的:
public class Card
{
private int value;
private int suit;
public Card(int ranvalue, int ransuit)
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
var values = new []
{
"ace", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king"
};
var suits = new []
{
"Hearts", "Spades", "Diamonds", "Clubs"
};
return String.Format("{0} of {1}", values[value - 1], suits[suit - 1]);
}
}
public class Deck
{
private Random rng = new Random();
private Card[] deck;
public Deck()
{
deck =
(
from value in Enumerable.Range(1, 13)
from suit in Enumerable.Range(1, 4)
select new Card(value, suit)
).ToArray();
}
public string deal(int number)
{
string dealt = string.Join(" ", deck.OrderBy(x => rng.Next()).Take(number));
return dealt;
}
}
这可以修复重复的卡片问题。