public class DeckOfCards{
public static void main(String[] args) {
String[] s = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
String[] face = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
int n = s.length * face.length;
String[] deck = new String[n];
for (int i = 0; i < face.length; i++) {
for (int j = 0; j < s.length; j++) {
deck[s.length*i + j] = face[i] + ", " + s[j];
}
}
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
for (int i = 0; i < n; i++) {
System.out.println(deck[i]);
}
}
}
答案 0 :(得分:1)
您可以创建一个Card类,然后将其添加到卡片的arraylist中。这个类可以封装每张卡的Rank,Suit和Face信息。
class Card {
private ArrayList≤Integer≥ rank = new ArrayList<>();
private String suit;
private String face;
public Card(int newRank, String newSuit, String newFace){
this.rank.add(newRank);
this.suit = newSuit;
this.face = newFace;
}
//additional methods, getters/setters.
}
您可以通过创建卡片的arraylist来填充您的套牌。
Arraylist<Card> deck = new ArrayList<>();
然后通过迭代数组添加到你的套牌。
for(int j = 0; j < suitArray.length; j++){
for(int k = 0; k < faceArray.length; k++){
deck.add(new Card(k, suitArray[j], faceArray[k]));
}
}
这仅适用于增量排名,即ACE = 11,King = 10 .. ONE = 1。
如果你需要不同的等级权重,你可以在填充牌组后尝试检查你想要分配额外重量的牌。
for(Card c : deck){
if (c.getName.equals("ACE"))
c.addRank(11);
}
}
答案 1 :(得分:0)
最简单的方法是创建额外的数组来存储排名,例如:
String[] ranks = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13"};
并将甲板人口更新为:
deck[s.length*i + j] = face[i] + ", " + s[j] + ", rank: " + ranks[i];
更好的解决方案是创建Card类来存储所有数据。