所以我的代码只有一个问题,那就是检查同一张卡,因为我不希望这些卡重复。没有任何重新洗牌,它只是随机牌,直到没有剩下。我不清楚如何做到这一点。我很感激一些帮助;无论是theanswe还是只是一点点轻推。
package Card;
import java.util.Random;
public class deckOfCards {
public String[] suite = { "Hearts", "Spade", "Diamonds", "Clubs" };
public String[] faceValue = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
public int deckCount = 0;
private Card[] deck = new Card[52];
public int index;
public void buildDeck() {
for (int i = 0; i < suite.length; i++) {
for (int j = 0; j < faceValue.length; j++) {
deck[deckCount] = new Card(suite[i], faceValue[j]);
deckCount++;
}
}
}
public Card shuffle(){
Random rand = new Random();
int index = rand.nextInt(52);
return deck[index];
}
}
这会创建卡并随机化它们 包卡;
public class Card {
private String suite;
private String faceValue;
public Card(){
}
public Card(String suite, String faceValue){
this.setSuite(suite);
this.setFaceValue(faceValue);
}
public String getSuite(){
return suite;
}
//array list
//
public void setSuite(String suite){
this.suite = suite;
}
public String getFaceValue(){
return faceValue;
}
public void setFaceValue(String faceValue){
this.faceValue = faceValue;
}
}
package Card;
public class Driver {
public deckOfCards cards = new deckOfCards();
private Card [] discard = new Card[52];
public static void main(String[] args) {
Driver driver = new Driver();
driver.DealCards();
}
public void DealCards(){
int cardsLeft = 52;
cards.buildDeck();
Card randomCard = new Card();
for (int i = 0; i < 5; i++) {
cardsLeft--;
randomCard = cards.shuffle();
System.out.println(randomCard.getFaceValue() + " of " + randomCard.getSuite());
}
System.out.println("Cards left:" + cardsLeft);
}
}
这会发卡。再次,任何帮助表示赞赏。
答案 0 :(得分:0)
我认为缺少的是一种从牌组中移除牌的方法。
每当发牌时,需要将其移除,并在剩余牌组上执行新的随机抽牌,而不是每次都在52张牌上。这称为无需替换的绘制。当前代码执行抽奖与替换。
实现此目的的最快方法是仅修改deckOfCards
类: