我正在尝试创建一个 OOP 友好的 Java BlackJack 游戏来提高我的知识。
我碰壁了,我只是不知道怎么看问题。想知道是否有人可以指出我的问题。
此外,在谷歌搜索相关主题后,我发现人们一次又一次地说使用枚举会更有益,作为初学者会被建议这样做吗?或者我应该暂时坚持使用字符串数组。
谢谢你。
我的代码:
public class BlackJack{
BlackJack() {
Deck deck = new Deck();
deck.createDeck();
System.out.println(deck.deckList);
}
public static void main(String[] args) {
new BlackJack();
}
}
public class Card{
private String valueCard;
private String suitCard;
public Card(String value, String suit) {
this.valueCard = value;
this.suitCard = suit;
}
public String getValue() {
return valueCard;
}
public String getSuit() {
return suitCard;
}
}
import java.util.ArrayList;
public class Deck {
ArrayList<Card> deckList = new ArrayList<Card>();
String[] value = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"};
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
Deck() {
deckList = new ArrayList<Card>();
}
public void createDeck() {
for (int i = 0; i < value.length; i++) {
for (int x = 0; x < suit.length; x++) {
Card card = new Card(value[i], suit[x]);
deckList.add(card);
}
}
}
}
编辑:目前我的 println 输出是:[Card@addbf1, Card@42e816, Card@9304b1, ... etc] 这是什么意思?
感谢您的时间。
编辑:将来还需要回答这个问题的任何人:
添加:
@Override
public String toString(){
return valueCard + " of " + suitCard;
}
}
到我的 Card 类,然后在 Deck 类中使用它:
import java.util.ArrayList;
public class Deck {
ArrayList<Card> deckList = new ArrayList<Card>();
String[] value = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"};
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
Deck() {
deckList = new ArrayList<Card>();
}
public void createDeck() {
for (int i = 0; i < value.length; i++) {
for (int x = 0; x < suit.length; x++) {
Card card = new Card(value[i], suit[x]);
deckList.add(card);
card.toString();
}
}
}
}
枚举:公共类 CardEnum {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
}
public class Card{
private CardEnum.Rank rank;
private CardEnum.Suit suit;
public Card(CardEnum.Rank rank, CardEnum.Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
@Override
public String toString(){
return rank + " of " + suit;
}
}