我正在创建一副 20 张卡片。他们每个人都从 1 到 10 分配了整数总统。甲板应如下所示: 1, 1, 2, 2, 3, 3, ... 17, 17, 18, 18, 19, 19, 20, 20
包含搜索表明它每次都是套牌中的一张新牌。我相信我的 equals() 方法可能有问题,但我不确定。有任何想法吗?
//类主类
public void createDeck() {
cards = new ArrayList<President>();
President temp;
for (int i = 1; i <= 20; i++) {
do {
temp = new President(i, rand(20));
System.out.println(cards.contains(temp));
} while (cards.contains(temp));
cards.add(temp);
System.out.println(cards.size());
}
for(President p : cards){
while(p.getPresident() > 10){
p.setPresident(p.getPresident() - 10);
}
System.out.println("" + p.getPresident());
}
}
//班长
public class President {
private int president;
private int card;
public President(int card, int president) {
super();
this.card = card;
this.president = president;
}
@Override
public boolean equals(Object o) {
if(o instanceof President){
President p = (President) o;
if(p.getPresident() == this.president && p.getCard() == this.card){
return true;
}
}
return false;
}
private int getCard() {
// TODO Auto-generated method stub
return card;
}
public int getPresident() {
// TODO Auto-generated method stub
return president;
}
public void setPresident(int president) {
// TODO Auto-generated method stub
this.president = president;
}
}