gpt4 book ai didi

java - 使用二维数组在java中创建一副纸牌

转载 作者:行者123 更新时间:2023-11-30 02:48:54 24 4
gpt4 key购买 nike

我正在尝试创建一个套牌并为每个套牌应用一套,例如:

cardValue[0][1] 并将其转换为 cardValue[Spade][1]

但我不明白如何转换它。我目前拥有的:

        int cardValues[][];
for(int a = 0; a <= 3; a++)
{
for(int b = 0; b <= 12; b++)
{
cardValues[][] += cardValues[a][b];
if(cardValues[a][b] == cardValues[a][0])
{
cardValues[a][b] = 5;
}
else if(cardValues[a][b] > cardValues[a][0] || cardValues[a][b] < cardValues[a][10])
{
cardValues[a][b] = 1;
}
else if(cardValues[a][b] == cardValues[a][10])
{
cardValues[a][b] = 2;
}
else if(cardValues[a][b] == cardValues[a][11])
{
cardValues[a][b] = 3;
}
else if(cardValues[a][b] == cardValues[a][12])
{
cardValues[a][b] = 4;
}
}
}

我不明白如何将 0、1、2、3 转换为黑桃、方 block 、梅花、红桃。

最佳答案

我会做类似的事情:

Suit.java

public enum Suit {
SPADES, DIAMONDS, CLUBS, HEARTS
}

Card.java

public class Card {

private Suit suit;
private String value;

public Card(Suit suit, String value) {
this.suit = suit;
this.value = value;
}

public Suit getSuit() {
return suit;
}
public void setSuit(Suit suit) {
this.suit = suit;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}

}

Main.java

public class Main {

public static void main(String[] args) {

String[] values = new String[]{"K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"};

Card[] deck = new Card[52];

int i = 0;
for (Suit suit : Suit.values()) {
for (String value : values) {
deck[i++] = new Card(suit, value);
}
}

for (Card card : deck) {
System.out.println(card.getValue() + " of " + card.getSuit());
}

}

}

关于java - 使用二维数组在java中创建一副纸牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312091/

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