我正在創建一張應該在內置卡片時顯示所有52張卡片的實例卡片,然後它應該洗牌並處理5.此後,菜單會提示另外5個卡片進行處理,被重新洗牌,這會將所有已提取的卡片添加回去,或退出應用程序。但是,當我運行該應用程序時,它僅顯示所有52張卡片的空值以及每張發出的卡片。在我和我的導師談話之前,應用程序運行並且幾乎滿足了所有的規範,而我現在擁有的是我們談話期間/之後的地方。我不確定我做錯了什麼,可以真正使用幫助。爲了澄清,我將發佈要求,然後是代碼。我很感激幫助。不顯示卡片
At startup, it constructs a Deck of Cards
52 distinct cards running from Ace - King, of suit Heart, Club, Diamond, or Spade
Shuffle the cards!!
For proof, print out all 52 cards to the console in a useful, readable way.
Then, deal the top five cards to the console (meaning print them to the console)
After all of this, allow the user choose between dealing the next five cards, reshuffling the deck, or quitting the application.
If the user chooses to deal the next 5 cards, do so based on which cards have not yet been dealt. DO NOT RESHUFFLE.
If the user chooses to reshuffle, simply repeat the process of shuffling, printing the deck, and dealing the top five cards.
If the user chooses to quit the application, simply end the app.
代碼我正在練習封裝,所以我會發布每個類在eclipse中分解。
驅動
public class Driver {
public static void main(String[] args) throws IOException {
DeckRun.run();
}
}
DeckRun
public class DeckRun {
static Card[] d1 = new Card[52];
public static void run() throws IOException {
printDeck();
System.out.println("");
Deal.dealCards(d1);
System.out.println("");
menu();
}
public static void printDeck() {
for (Card c : d1) {
System.out.println(c);
}
}
public static void menu() throws IOException{
BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));
int menu = 0;
do {
System.out.println("Press 1 to be dealt 5 random cards.");
System.out.println("Press 2 to shuffle all the cards back into the deck.");
System.out.println("Press 3 to quit the application.");
String input = readRacer.readLine();
menu = Integer.parseInt(input);
switch (menu) {
case 1: Deal.dealCards(d1);
break;
case 2: System.out.println("The deck has been shuffled.");
Deck[] d1 = new Deck[52];
break;
case 3: System.out.println("I'm not bad, I'm just drawn that way.");
break;
}
} while (menu != 3);
}
}
卡
public class Card {
private Rank rank; // Variable to assign a card its rank.
private Suit suit; // Variable to assign a card its suit.
public Card (Rank rank, Suit suit) { // Constructor to build a card.
this.rank = rank;
this.suit = suit;
}
public Rank getRank() { // Retrieves the card's rank from the enum Rank.
return rank;
}
public Suit getSuit() { // Retrieves the card's suit from the enum Suit.
return suit;
}
@Override
public String toString() { //
return rank + " OF " + suit;
}
}
甲板
public class Deck {
private Card[] cards;
public Deck() {
int numberOfRanks = 13;
int numberOfSuits = 4;
int numberOfCards = numberOfRanks * numberOfSuits;
Rank[] rank = Rank.values();
Suit[] suit = Suit.values();
cards = new Card[numberOfCards];
for (int i = 0; i < numberOfRanks; i++) {
for (int j = 0; j < numberOfSuits; j++) {
cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
}
}
}
public void shuffleCards() {
Random rand = new Random();
for (int c = rand.nextInt(6) + 5; c > 0; c--) {
for (int i = cards.length - 1; i > 0; i--) {
int index = rand.nextInt(i + 1);
Card card = cards[index];
cards[index] = cards[i];
cards[i] = card;
}
}
}
}
新政
public class Deal {
private static int counter = 0;
public static void dealCards(Card[] d1) {
for (int i = 0; i < 5; i++) {
counter++;
System.out.println(d1[counter]);
if (counter == 50) {
System.out.println("Almost all cards have been used, please reshuffle.");
}
}
}
}
排名(枚舉)
public enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING, }
套裝(枚舉)
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES, }
不能確定其中的問題,但我現在已經改變了,是實現我後需要一個甲板的實例,我的教授幫助我理解,而不是甲板上的一副紙牌的方法,我需要一個構造函數。我相信我正走在正確的道路上,但我完全陷入了困境。任何幫助表示讚賞,如果你能解釋我的錯誤,所以我可能會學習和進步更多的感謝。
我會給這個去,但我不完全確定我明白你的意思。不過,我會檢查這一點,並盡我所能。 –
因此,通過這個和其他一些調整我已經完成了,除了不重複規則之外,我現在可以完成所有工作。另外,我不知道如何處理隨機播放選項。我想我應該創建一個甲板的新實例,但是,然後我需要一種方法來取代每一個調用第一個實例的命令與第二個...我現在很迷茫和困惑.. –