我正在尝试在Pack类中测试我的两个函数,以模拟一副纸牌。
public class Pack {
private PlayingCard[] deck; // An array of 52 cards, representing the deck.
private int cardsUsed; // How many cards have been dealt from the deck.
/**
* Creating an unshuffled deck of cards
*/
public Pack() {
deck = new PlayingCard[51]; //Creates an array of 52 playing cards
int cardCt = 0; // How many cards have been created so far.
for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
for ( int value = 1; value <= 14; value++ ) { //Builds a complete suit
deck[51] = new PlayingCard(value,suit);
cardCt++; //Adds one to the card count
}
}
cardCt = 0;
}
/**
* Shuffling a deck of cards
*/
public void shuffle() {
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
PlayingCard temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public @Override String toString()
{
return Pack();
}
} // end class Pack
我希望它看起来类似于我完成我的PlayingCard类的方式,以使其更加正式。
public class PlayingCard {
/**
* Declaring Variables
*/
private int rank;
private int suit;
/**
* Declaring Non-Numerical Cards
*/
public static final int jack = 11;
public static final int queen = 12;
public static final int king = 13;
public static final int ace = 14;
/**
* Declaring Suits
*/
public static final int clubs = 0;
public static final int diamonds = 1;
public static final int hearts = 2;
public static final int spades = 3;
/**
* Constructs a card with specified initial rank and suit
*/
public PlayingCard(int rank, int suit)
{
this.rank=rank;
this.suit=suit;
}
public int getSuit()
{
return suit;
}
public int getRank()
{
return rank;
}
/**
* Switch Statements are use because there are many execution paths
*/
public String getSuitAsString()
{
switch(suit)
{
case clubs: return "Clubs";
case diamonds: return "Diamonds";
case hearts: return "Hearts";
case spades: return "Spades";
default: return "ERR";
}
}
/**
* Switch Statements are use because there are many execution paths
*/
public String getRankAsString()
{
switch(rank)
{
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
case 14: return "Ace";
default: return "ERR";
}
}
/**
* toString Method
*/
public @Override String toString()
{
return getRankAsString() + " of " + getSuitAsString();
}
}
我想做的就是将shuffle方法放到toString方法中,例如PlayingCard类,这样我就可以在PackTester中对其进行测试。我知道我无法返回void语句,这就是我遇到的问题。如果有人可以解释该怎么做,我将非常感激。
最佳答案
你能猜出我在做什么吗? :)
public class Pack {
// .. other code ..
public @Override String toString() {
// see note below
// shuffle();
StringBuilder sb = new StringBuilder();
sb.append("The cards are: ");
for (int i = 0; i < deck.length; i++) {
if (i > 0) sb.append(", ");
sb.append(deck.toString());
}
sb.append(".");
return sb.toString();
}
}
注意:如果需要,可以在此处调用
shuffle
方法来洗牌,但这是不好的设计,因为每次要打印卡座时,都需要对其进行洗牌。 toString
方法应仅告诉您当前状态,而不更改状态。理想情况下,您应该执行以下操作:
Pack myPack = new Pack();
myPack.shuffle();
System.out.println(myPack); // this automatically calls toString()
请注意,
void
返回类型表示没有对象返回-该方法在被调用时会执行其操作。