2015-11-18 40 views
1

我在初學者的java課程中。我必須創建一張卡片。然後創建一副牌和一些方法,然後測試它。在輸出中爲空而其他人不具有nulll

我的卡類是如下

import java.util.*; 

public class Card 
{ 
    public final static int ACE = 1; 
    public final static int TWO = 2; 
    public final static int THREE = 3; 
    public final static int FOUR = 4; 
    public final static int FIVE = 5; 
    public final static int SIX = 6; 
    public final static int SEVEN = 7; 
    public final static int EIGHT = 8; 
    public final static int NINE = 9; 
    public final static int TEN = 10; 
    public final static int JACK = 11; 
    public final static int QUEEN = 12; 
    public final static int KING = 13; 

    public final static int CLUBS = 1; 
    public final static int DIAMONDS = 2; 
    public final static int HEARTS = 3; 
    public final static int SPADES = 4; 

    private final static int NUM_FACES = 13; 
    private final static int NUM_SUITS = 4; 

    private int face, suit; 
    private String faceName, suitName; 

    private int myInt1, myInt2; 

    Random rand = new Random(); 

    //----------------------------------------------------------------- 
    // Creates a random card. 
    //----------------------------------------------------------------- 
    public Card() 
    { 
     face = rand.nextInt(4) + 1; 
     setFaceName(); 

     suit = rand.nextInt(13) + 1; 
     setSuitName(); 
    } 



    //----------------------------------------------------------------- 
    // Sets the string representation of the face using its stored 
    // numeric value. 
    //----------------------------------------------------------------- 
    private void setFaceName() 
    { 
     switch (face) 
     { 
     case 1: 
      faceName = "Ace"; 
      break; 
     case 2: 
      faceName = "Two"; 
      break; 
     case 3: 
      faceName = "Three"; 
      break; 
     case 4: 
      faceName = "Four"; 
      break; 
     case 5: 
      faceName = "Five"; 
      break; 
     case 6: 
      faceName = "Six"; 
      break; 
     case 7: 
      faceName = "Seven"; 
      break; 
     case 8: 
      faceName = "Eight"; 
      break; 
     case 9: 
      faceName = "Nine"; 
      break; 
     case 10: 
      faceName = "Ten"; 
      break; 
     case 11: 
      faceName = "Jack"; 
      break; 
     case 12: 
      faceName = "Queen"; 
      break; 
     case 13: 
      faceName = "King"; 
      break; 
     } 
    } 

    //----------------------------------------------------------------- 
    // Sets the string representation of the suit using its stored 
    // numeric value. 
    //----------------------------------------------------------------- 
    private void setSuitName() 
    { 
     switch (suit) 
     { 
     case 1: 
      suitName = "Clubs"; 
      break; 
     case 2: 
      suitName = "Diamonds"; 
      break; 
     case 3: 
      suitName = "Hearts"; 
      break; 
     case 4: 
      suitName = "Spades"; 
      break; 
     } 
    } 

    //----------------------------------------------------------------- 
    // Determines if this card is higher than the passed card. The 
    // second parameter determines if aces should be considered high 
    // (beats a King) or low (lowest of all faces). Uses the suit 
    // if both cards have the same face. 
    //----------------------------------------------------------------- 
    public boolean isHigherThan (Card card2, boolean aceHigh) 
    { 
     boolean result = false; 

     if (face == card2.getFace()) 
     { 
     if (suit > card2.getSuit()) 
      result = true; 
     } 
     else 
     { 
     if (aceHigh && face == ACE) 
      result = true; 
     else 
      if (face > card2.getFace()) 
       result = true; 
     } 

     return result; 
    } 

    //----------------------------------------------------------------- 
    // Determines if this card is higher than the passed card, 
    // assuming that aces should be considered high. 
    //----------------------------------------------------------------- 
    public boolean isHigherThan (Card card2) 
    { 
     return isHigherThan (card2, true); 
    } 

    //----------------------------------------------------------------- 
    // Returns the face (numeric value) of this card. 
    //----------------------------------------------------------------- 
    public int getFace() 
    { 
     return face; 
    } 

    //----------------------------------------------------------------- 
    // Returns the suit (numeric value) of this card. 
    //----------------------------------------------------------------- 
    public int getSuit() 
    { 
     return suit; 
    } 

    //----------------------------------------------------------------- 
    // Returns the face (string value) of this card. 
    //----------------------------------------------------------------- 
    public String getFaceName() 
    { 
     return faceName; 
    } 

    //----------------------------------------------------------------- 
    // Returns the suit (string value) of this card. 
    //----------------------------------------------------------------- 
    public String getSuitName() 
    { 
     return suitName; 
    } 

    //----------------------------------------------------------------- 
    // Returns the string representation of this card, including 
    // both face and suit. 
    //----------------------------------------------------------------- 



    public String toString() 
    { 
     return getFaceName() + " of " + getSuitName(); 
    } 
} 

我的卡類的甲板如下

import java.util.*; 


public class DeckOfCards 
{ 

    private Card deckOfCards[]; 
    private int currentCardUsed; 
    private final int NumberOfCards = 52; 

    private int nextCard; 

    private Random rand; 

    String myString = "All Cards have been dealt."; 




    public DeckOfCards() 
    { 
     deckOfCards = new Card[NumberOfCards]; 
     currentCardUsed = 0; 

     rand = new Random(); 

     for(int index = 0; index < deckOfCards.length; index ++) 
     { 
      deckOfCards[index] = new Card(); 
     } 
    } 

    public void shuffleCards() 
    { 
      currentCardUsed = 0; 

      for(int newCard = 0; newCard < deckOfCards.length; newCard ++) 
      { 
       int nextCard = rand.nextInt(NumberOfCards); 

       Card temporaryDeck = deckOfCards[newCard]; 
       deckOfCards[newCard] = deckOfCards[nextCard]; 
       deckOfCards[nextCard] = temporaryDeck; 
      } 
    } 

    public Card dealCard() 
    { 
     if(currentCardUsed < deckOfCards.length) 
     { 
      return deckOfCards[currentCardUsed ++]; 
     } 
     else 
     { 
      return null; 
     } 
    } 
} 

而我的驅動類如下

public class DeckTester 
{ 
    public static void main(String [] args) 
    { 
     DeckOfCards deck = new DeckOfCards(); 

     deck.shuffleCards(); 

     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 

     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 
     System.out.println(deck.dealCard()); 



    } 
} 

輸出:

Two of null 
Ace of Hearts 
Two of null 
Four of null 
Ace of null 
Ace of Spades 
Two of Diamonds 
Ace of Clubs 
Ace of null 

任何幫助,爲什麼不是所有的卡都值將不勝感激,

+0

沒有問題,但你可以讓你的代碼*很多*清潔劑具有弦的臉和西裝一類級別的陣列,然後就做這樣的事情'私人無效setFaceName(){面名=面[ face - 1]; }'。 – paxdiablo

+0

我改變了代碼段,但現在所有的輸出都是空白的,不僅僅是一些。 –

+0

我修好了。謝謝大家的幫助 –

回答

1

簡單的錯誤:

變化

public Card() 
    { 
     face = rand.nextInt(4) + 1; 
     setFaceName(); 

     suit = rand.nextInt(13) + 1; 
     setSuitName(); 
    } 

public Card() 
    { 
     face = rand.nextInt(13) + 1; 
     setFaceName(); 

     suit = rand.nextInt(4) + 1; 
     setSuitName(); 
    } 

其他建議:

  • 使用枚舉,而不是魔術數字,你不會有這個爛攤子。
  • 不要創建隨機卡片,創建一個完整的卡片並隨機播放。
0

有13面,4套(你甚至定義了常量)。所以,

face = rand.nextInt(4) + 1; 
// ... 
suit = rand.nextInt(13) + 1; 

應該

face = rand.nextInt(13) + 1; 
// ... 
suit = rand.nextInt(4) + 1; 

使用先前定義的常量Card,像

face = rand.nextInt(Card.NUM_FACES) + 1; 
// ... 
suit = rand.nextInt(Card.NUM_SUITS) + 1; 
0

更新卡的構造與下面的代碼。

public Card() { 
     Random r = new Random(); 
     int Low = 1; 
     int High = 13; 
     face = r.nextInt(High - Low) + Low; 
     setFaceName(); 

     Random r2 = new Random(); 
     int Low2 = 1; 
     int High2 = 4; 
     suit = r2.nextInt(High2 - Low2) + Low2; 
     setSuitName(); 
    } 
相關問題