为什么我只得到NULL?

时间:2016-05-05 02:20:45

标签: java

当我运行该程序时,我所有的回复都是空的...而且我不能再盯着这个了。我似乎无法找到错误......有人可能只是提示一下......我不知道其他相关细节是什么。

 public class Deckofcards
   {
      public static final int NCARDS = 52;

      private card[] deckOfCards;         // Contains all 52 cards
      private int currentCard;            // deal THIS card in deck           

      public Deckofcards( )    // Constructor
      {
     deckOfCards = new card[ NCARDS ];

     int i = 0;

     for ( int suit = card.SPADE; suit <= card.DIAMOND; suit++ )
        for ( int rank = 1; rank <= 13; rank++ )
        deckOfCards[i++] = new card(suit, rank);

      private byte cardSuit;
      private byte cardRank;

      public card( int suit, int rank )
      {
     if ( rank == 1 )
        cardRank = 14;     // Give Ace the rank 14
     else
        cardRank = (byte) rank;

     cardSuit = (byte) suit;
      }

      public boolean equals( card x )
      {
     if ( this.cardSuit == x.cardSuit &&
          this.cardRank == x.cardRank    )
        return ( true ); 
     else         
        return ( false );
      }

      public String toString()
      {
     return ( Rank[ cardRank ] + Suit[ cardSuit ] );
      }
   }


    public class Dealer

   {
      public static void main(String[] args)
      {

     Deckofcards a;

     a = new Deckofcards();
     System.out.println(a);      // What a new deck look like     

     System.out.println("Shuffle cards....");            
     a.shuffle(1000);            // Shuffle deck of card "a"
     System.out.println(a);      // Deck after shuffling

     card b;

     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);

      }
   }    
     currentCard = 0;
      }

       //shuffle(n): shuffle the deck

      public void shuffle(int n)
      {
     int i, j, k;

     for ( k = 0; k < n; k++ )
     {
         i = (int) ( NCARDS * Math.random() );  // Pick 2 random cards
         j = (int) ( NCARDS * Math.random() );  // in the deck

        //swap these randomly picked cards

         card tmp = deckOfCards[i];
         deckOfCards[i] = deckOfCards[j];
         deckOfCards[j] = tmp;;
     }

     currentCard = 0;   // Reset current card to deal
      }

     //deal(): deal deckOfCards[currentCard] out

      public card deal()
      {
     if ( currentCard < NCARDS )
     {
        return ( deckOfCards[ currentCard++ ] );
     }
     else
     {
        System.out.println("Out of cards error");
        return ( null );  // Error;
     }
      }

      public String toString()
      {
     String s = "";
     int k;

     k = 0;
     for ( int i = 0; i < 4; i++ )
     {
        for ( int j = 1; j <= 13; j++ )
        s += (deckOfCards[k++] + " ");

        s += "\n";
     }
     return ( s );
      }
   }


    public class card
   {
      public static final int SPADE   = 4;
      public static final int HEART   = 3;
      public static final int CLUB    = 2;
      public static final int DIAMOND = 1;

      private static final String[] Suit = { "*", "d", "c", "h", "s"};
      private static final String[] Rank = { "*", "1", "2", "3", "4",
               "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

      private byte cardSuit;
      private byte cardRank;

      public card( int suit, int rank )
      {
     if ( rank == 1 )
        cardRank = 14;     // Give Ace the rank 14
     else
        cardRank = (byte) rank;

     cardSuit = (byte) suit;
      }

      public boolean equals( card x )
      {
     if ( this.cardSuit == x.cardSuit &&
          this.cardRank == x.cardRank    )
        return ( true ); 
     else         
        return ( false );
      }

      public String toString()
      {
     return ( Rank[ cardRank ] + Suit[ cardSuit ] );
      }
   }


    public class Dealer

   {
      public static void main(String[] args)
      {

     Deckofcards a;

     a = new Deckofcards();
     System.out.println(a);      // What a new deck look like     

     System.out.println("Shuffle cards....");            
     a.shuffle(1000);            // Shuffle deck of card "a"
     System.out.println(a);      // Deck after shuffling

     card b;

     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);
     b = a.deal();
     System.out.println("Deal a card: " + b);

      }
   }

这是我收到的内容。

null null null null null null null null null null null null null 

null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null

随机牌.... null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null

交易卡:null 交易卡:null 交易卡:null 交易卡:null 交易卡:null

1 个答案:

答案 0 :(得分:-1)

发现我的错误......

在我的Deck of cards.java文件中......钻石和Spade位于错误的位置....下面是更正的部分。

for ( int suit = card.DIAMOND; suit <= card.SPADE; suit++ )
        for ( int rank = 1; rank <= 13; rank++ )
        deckOfCards[i++] = new card(suit, rank);