我已经写了3个类。一个是卡类,代表扑克牌:
public class Card
{
int theRank, theSuit, theCard;
public Card(int suit, int rank)
{
theRank = rank;
theSuit = suit;
}
public String getRank()
{
String rankString = "";
switch (theRank)
{
case 1:
rankString = "Ace";
case 2:
rankString = "2";
case 3:
rankString = "3";
case 4:
rankString = "4";
case 5:
rankString = "5";
case 6:
rankString = "6";
case 7:
rankString = "7";
case 8:
rankString = "8";
case 9:
rankString = "9";
case 10:
rankString = "10";
case 11:
rankString = "Jack";
case 12:
rankString = "Queen";
case 13:
rankString = "King";
}
return rankString;
}
public String getSuit()
{
String suitString = "";
switch(theSuit)
{
case 1:
suitString = "Diamonds";
case 2:
suitString = "Hearts";
case 3:
suitString = "Clubs";
case 4:
suitString = "Spades";
}
return suitString;
}
}
另一个是类 Deck,它表示 Card 对象的数组列表:
public class Deck
{
public ArrayList<Card> loadDeck(ArrayList<Card> deck)
{
for (int suit = 1; suit <= 4; suit++)
{
for (int rank = 1; rank <= 13; rank++)
{
deck.add(new Card(suit, rank));
}
}
return deck;
}
public void printDeck(ArrayList<Card> deck)
{
for (int i = 0; i <= deck.size(); i++)
{
Card temp = deck.get(i);
String rank = temp.getRank();
String suit = temp.getSuit();
System.out.println(rank + " of " + suit);
}
}
public void shuffleDeck(ArrayList<Card> deck)
{
Card temp;
int index;
Random rand = new Random();
for (int i = deck.size() - 1; i > 0; i--)
{
index = rand.nextInt(i + 1);
temp = deck.get(index);
deck.add(index, deck.get(i));
temp = deck.get(i);
}
}
public ArrayList dealHand(ArrayList<Card> deck)
{
Random rand = new Random();
ArrayList<Card> hand = new ArrayList<Card>(8);
for (int i = 0; i <= 6; i++)
{
int suit = rand.nextInt(3);
int rank = rand.nextInt(51);
Card temp = new Card(suit, rank);
hand.add(i, temp);
deck.remove(temp);
}
return hand;
}
public void printHand(ArrayList<Card> hand)
{
for (int i = 0; i <= 6; i++)
{
Card temp = hand.get(0);
System.out.println(temp.getRank() + " of " + temp.getSuit());
}
}
}
然后我有一个包含主方法的类 GoFish:
import java.util.ArrayList;
public class GoFish
{
public static void main(String[] args)
{
Deck testDeck = new Deck();
ArrayList<Card> deck = new ArrayList<Card>();
deck = testDeck.loadDeck(deck);
testDeck.printDeck(deck);
}
}
我想要的输出是标准 52 副牌中每张牌的等级和花色。但是填充 arrayList 或打印列表时存在问题。有什么建议吗?
如@pbabcdefp所述,您的 switch 语句缺少其中断语句。如果没有中断,执行将通过最后一个 case 语句设置 rankString = "King"。这是切换到教程的链接
它应该是:
switch (theRank) {
case 1:
rankString = "Ace";
break;
case 2:
rankString = "2";
break;
case 3:
rankString = "3";
break;
case 4:
rankString = "4";
break;
case 5:
rankString = "5";
break;
case 6:
rankString = "6";
break;
case 7:
rankString = "7";
break;
case 8:
rankString = "8";
break;
case 9:
rankString = "9";
break;
case 10:
rankString = "10";
break;
case 11:
rankString = "Jack";
break;
case 12:
rankString = "Queen";
break;
case 13:
rankString = "King";
break;
}
同样在方法printDeck(ArrayList)中,抛出了一个IndexOutOfBoundsException,因为像普通的Java数组一样,列表是从零开始索引的 https://docs.oracle.com/javase/7/docs/api/java/util/List.html#get(int)所以
for (int i = 0; i <= deck.size(); i++)
应该是
for (int i = 0; i < deck.size(); i++)
case
的每一端都需要break;