我正在设计黑杰克计划。我的问题是,我使用一个开关盒来生成随机卡。但是,当需要比较卡片的价值时......让我们说if(pCard1 + pCard2 > 21)
它不允许我比较它们,因为它们是字符串。但是,他们需要成为字符串,因为我必须能够分配String "Queen"
,但我还需要" Queen" int值为10.我不知道该怎么做。
这是我到目前为止所拥有的。我不包括我的整个代码,因为它长了几百行。仅包括二十一点部分。
System.out.println("---------------------Black Jack--------------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Available balance is $"+balance);
System.out.print("How much would you like to bet on this hand?: ");
int bet = input.nextInt();
balance -= bet;
System.out.println("You just bet $"+bet+"......Dealing cards!");
System.out.println("----------------------------------------------------------");
String pCard1 = dealCard();
String pCard2 = dealCard();
System.out.println("Your hand is a "+pCard1+" and "+pCard2);
System.out.print("Would you like to Hit or Stand? :");
String hOs = input.next();
if(hOs.equals("Hit")) {
String pCard3 = dealCard();
if(pCard1 + pCard2 + pCard3);
}
}
}
public static String dealCard() {
int value = (int)(Math.random() * 13);
String returnString = "";
switch ( value ) {
case 1: returnString = "Ace"; break;
case 2: returnString = "2"; break;
case 3: returnString = "3"; break;
case 4: returnString = "4"; break;
case 5: returnString = "5"; break;
case 6: returnString = "6"; break;
case 7: returnString = "7"; break;
case 8: returnString = "8"; break;
case 9: returnString = "9"; break;
case 10: returnString = "10"; break;
case 11: returnString = "Jack"; break;
case 12: returnString = "Queen"; break;
case 13: returnString = "King"; break;
}
return returnString;
}
}
答案 0 :(得分:3)
我建议使用Enum,这样您就可以轻松比较卡片了。 你想要的enum课程将是:
public enum Cards {
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)
;
private final int code;
Cards(int code){
this.code = code;
}
public Integer value(){
return code;
}
public static Cards getRelatedEnum(Integer code){
if(code == null)
return null;
switch (code){
case 1: return ace;
case 2: return two;
case 3: return three;
case 4: return four;
case 5: return five;
case 6: return six;
case 7: return seven;
case 8: return eight;
case 9: return nine;
case 10: return ten;
case 11: return jack;
case 12: return queen;
case 13: return king;
default: return null;
}
}
public static String toString(Cards card){
if(card == null)
return "";
switch (card){
case ace: return "ace";
case two: return "2";
case three: return "3";
case four: return "4";
case five: return "5";
case six: return "6";
case seven: return "7";
case eight: return "8";
case nine: return "9";
case ten: return "10";
case jack: return "jack";
case queen: return "queen";
case king: return "king";
default: return null;
}
}
}
然后您可以将dealCard()
方法更改为:
public static Cards dealCard() {
int value = (int)(Math.random() * 13);
return Cards.getRelatedEnum(value);
}
比较卡值使用类似:
public static void main(String[] args){
System.out.println("---------------------Black Jack--------------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Available balance is $"+balance);
System.out.print("How much would you like to bet on this hand?: ");
int bet = input.nextInt();
balance -= bet;
System.out.println("You just bet $"+bet+"......Dealing cards!");
System.out.println("-------------------------------------------");
Cards pCard1 = dealCard();
Cards pCard2 = dealCard();
System.out.println("Your hand is a "+pCard1.toString() +" and "+pCard2.toSring());
System.out.print("Would you like to Hit or Stand? :");
String hOs = input.next();
if(hOs.equals("Hit")) {
Cards pCard3 = dealCard();
if(pCard1.value() + pCard2.value() > pCard3.value());
}
}
答案 1 :(得分:3)
我认为枚举是解决此类问题的最佳方法。
如果我们为Suit和Pips定义枚举,那么
enum Suit {
Hearts, Diamonds, Clubs, Spades;
}
enum Pips {
Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King;
}
如果我们定义一个类卡:
class Card {
Suit suit;
Pips pips;
public Card(Suit suit, Pips pips) {
this.suit = suit;
this.pips = pips;
}
@Override
public String toString() {
return pips.toString() + " of " + suit.toString();
}
}
Card
使用toString()
将Suit和Pips转换为字符串,而使用ordinal()
可以获得零索引数值:
Card card = new Card(Suit.Spades, Pips.Ace);
System.out.println("Card: " + card);
System.out.println("Numeric suit: " + card.suit.ordinal());
System.out.println("Numeric pips: " + card.pips.ordinal());
这个简单的实现有一些限制。首先,诉讼或点数的名称只能是枚举名称的字符串版本。如果您希望UPPER CASE中的所有枚举都是好的风格怎么办?第二个问题是序数始终为零索引。如果你需要一些其他的id系统呢?
在这种情况下,我们将映射添加到每个枚举以保存这些映射。不幸的是,我们不能使用继承来在枚举之间共享代码,所以锅炉板有点罗嗦。
enum Suit {
HEARTS(1, "Hearts"), //
DIAMONDS(2, "Diamonds"), //
CLUBS(3, "Clubs"), //
SPADES(4, "Spades");
private static Map<String, Suit> nameToEnum = new HashMap<>();
private static Map<Integer, Suit> idToEnum = new HashMap<>();
static {
for (Suit suit : Suit.values()) {
nameToEnum.put(suit.getName(), suit);
idToEnum.put(suit.getId(), suit);
}
}
private int id;
private String name;
private Suit(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public static Suit fromName(String name) {
return nameToEnum.get(name);
}
public static Suit fromId(int id) {
return idToEnum.get(id);
}
}
enum Pips {
ACE(1, 1, "Ace"), //
TWO(2, 2, "Two"), //
THREE(3, 3, "Three"), //
FOUR(4, 4, "Four"), //
FIVE(5, 5, "Five"), //
SIX(6, 6, "Six"), //
SEVEN(7, 7, "Seven"), //
EIGHT(8, 8, "Eight"), //
NINE(9, 9, "Nine"), //
TEN(10, 10, "Ten"), //
JACK(11, 10, "Jack"), //
QUEEN(12, 10, "Queen"), //
KING(13, 10, "King");
private static Map<String, Pips> nameToEnum = new HashMap<>();
private static Map<Integer, Pips> idToEnum = new HashMap<>();
static {
for (Pips pips : Pips.values()) {
nameToEnum.put(pips.getName(), pips);
idToEnum.put(pips.getId(), pips);
}
}
private int id;
private int value;
private String name;
private Pips(int id, int value, String name) {
this.id = id;
this.value = value;
this.name = name;
}
public int getId() {
return id;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
public static Pips fromName(String name) {
return nameToEnum.get(name);
}
public static Pips fromId(int id) {
return idToEnum.get(id);
}
}
使用这些新枚举,我们会修改Card.toString()
以使用新的getName()
方法:
return pips.getName() + " of " + suit.getName();
现在,当我们使用卡片时,我们可以使用任何ID或名称或枚举,并在它们之间自由转换:
Card card = new Card(Suit.SPADES, Pips.ACE);
System.out.println("Card: " + card);
System.out.println("Numeric suit: " + card.suit.getId());
System.out.println("Numeric pips: " + card.pips.getId());
System.out.println("Suit by ID: " + Suit.fromId(3).getName());
System.out.println("Pips by Name: " + Pips.fromName("Ace").getName());
System.out.println("Pips value by Name: " + Pips.fromName("King").getValue());
答案 2 :(得分:2)
十,杰克,女王和国王都是不同的等级,但是它们的价值相同。由于它们都具有相同的价值,因此您需要的不仅仅是卡片的价值来确定它的等级。不可否认,您可以将Jack的值表示为11,将Queen表示为12等,然后将您的方法编码为将10以上的值识别为10,但这是错误的编码实践。
相反,你应该对每张卡中的等级和值进行编码(另外,你可能想要包括套装;虽然这对二十一点来说可能无关紧要)。
我建议制作一个具有String等级和整数值的Card类,而不是将您的卡表示为字符串。此外,这将是一个很好的机会来探索Java中的枚举,正如其他人评论的那样。