gpt4 book ai didi

java - 将 int 值赋给字符串

转载 作者:行者123 更新时间:2023-12-02 06:49:04 28 4
gpt4 key购买 nike

我正在设计一个 Black Jack 程序。我的问题是,我使用开关盒生成随机卡。但是当需要比较卡片的值(value)时......让我们说 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;
}
}

最佳答案

我认为枚举是解决此类问题的最佳方式。

如果我们像这样为 Suit 和 Pips 定义枚举

enum Suit {
Hearts, Diamonds, Clubs, Spades;
}

enum Pips {
Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King;
}

如果我们定义一个类 Card:

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());

这个简单的实现有几个限制。首先,花色或点数的名称只能是枚举名称的字符串版本。如果您希望所有枚举都采用大写形式,那会怎样?第二个问题是序数总是零索引的。如果您需要一些其他的 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());

关于java - 将 int 值赋给字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714583/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com