我正在尝试编写一个代码来显示一副牌中的所有卡片。不幸的是,最后添加到堆栈中的卡似乎一直覆盖所有先前输入的卡。我怎样才能防止这种情况发生?现在,当我输入代码时,它只会显示“黑桃 A”这一行 52 次。
import java.util.*;
public class cardSearcher{
public static void main(String[] args){
Stack<card> deck = new Stack<card>();
String[] value = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
String[] suit = {"Diamonds","Hearts","Clubs","Spades"};
for (int i=0;i<value.length;i++){
for (int j=0;j<suit.length;j++){
deck.push(new card(value[i],suit[j]));
}
}
while (!deck.empty()){
card chosenCard = deck.pop();
System.out.println(chosenCard.value +" of "+ chosenCard.suit);
}
}
}
这是卡代码:
public class card {
public static String value = "";
public static String suit = "";
public card(String valueofCard, String suitofCard) {
card.value = valueofCard;
card.suit = suitofCard;
}
}