在交易卡后返回卡片中的值

时间:2016-05-05 19:51:28

标签: java

尝试让我的代码在交易卡后返回卡片中留下的卡片数量。遇到一些问题......我无法让它发挥作用......这就是我所拥有的。我没有把经销商放进去,因为我还没有让它在这里工作......所以一旦我在这里工作,那么我会在我的经销商计划中将它添加到我的println。任何帮助表示赞赏...

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

     public card[] deckOfCards;         // Contains all 52 cards
     public int currentCard;            // deal THIS card in deck           
     public int cardsused;              // How many cards have been dealt
     public int cardsLeft;

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

     int i = 0;

     for ( int suit = card.DIAMOND; suit <= card.SPADE; suit++ )
        for ( int rank = 1; rank <= 13; rank++ )
        deckOfCards[i++] = new card(suit, rank);
     cardsused = 0;
     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
      }


            public int cardsLeft()
            {
                cardsused = currentCard++;
                return 52-cardsused;
            }


      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 );
      }

   }

以下是我的输出结果。

    Ace of Diamonds,  2 of Diamonds,  3 of Diamonds,  4 of Diamonds,  5 of Diamonds,  6 of Diamonds,  7 of Diamonds,  8 of Diamonds,  9 of Diamonds,  10 of Diamonds,  Jack of Diamonds,  Queen of Diamonds,  King of Diamonds,  
Ace of Clubs,  2 of Clubs,  3 of Clubs,  4 of Clubs,  5 of Clubs,  6 of Clubs,  7 of Clubs,  8 of Clubs,  9 of Clubs,  10 of Clubs,  Jack of Clubs,  Queen of Clubs,  King of Clubs,  
Ace of Hearts,  2 of Hearts,  3 of Hearts,  4 of Hearts,  5 of Hearts,  6 of Hearts,  7 of Hearts,  8 of Hearts,  9 of Hearts,  10 of Hearts,  Jack of Hearts,  Queen of Hearts,  King of Hearts,  
Ace of Spades,  2 of Spades,  3 of Spades,  4 of Spades,  5 of Spades,  6 of Spades,  7 of Spades,  8 of Spades,  9 of Spades,  10 of Spades,  Jack of Spades,  Queen of Spades,  King of Spades,  

Shuffling cards....
Ace of Clubs,  2 of Clubs,  4 of Spades,  Jack of Diamonds,  Queen of Clubs,  7 of Diamonds,  10 of Clubs,  Queen of Hearts,  Queen of Diamonds,  Jack of Spades,  4 of Diamonds,  6 of Diamonds,  9 of Spades,  
9 of Hearts,  7 of Clubs,  2 of Spades,  6 of Spades,  Ace of Spades,  2 of Diamonds,  King of Diamonds,  10 of Diamonds,  3 of Spades,  8 of Diamonds,  5 of Hearts,  3 of Diamonds,  6 of Hearts,  
10 of Hearts,  Jack of Hearts,  9 of Diamonds,  King of Hearts,  Jack of Clubs,  King of Spades,  9 of Clubs,  4 of Clubs,  7 of Hearts,  King of Clubs,  Queen of Spades,  3 of Clubs,  8 of Spades,  
5 of Spades,  Ace of Diamonds,  4 of Hearts,  Ace of Hearts,  3 of Hearts,  8 of Hearts,  8 of Clubs,  2 of Hearts,  6 of Clubs,  5 of Clubs,  10 of Spades,  7 of Spades,  5 of Diamonds,  

Deal a card: Ace of Clubs, 
Cards remaining: 
Deal a card: 2 of Clubs, 
Cards remaining: 
Deal a card: 4 of Spades, 
Cards remaining: 
Deal a card: Jack of Diamonds, 
Cards remaining: 
Deal a card: Queen of Clubs, 
Cards remaining: 

这是我的dealer.java

public class Dealer

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


     Deckofcards a;

     a = new Deckofcards();
     System.out.println(a);      // Prints what a new deck looks like     

     System.out.println("Shuffling cards....");            
     a.shuffle(1000);            // Shuffle deck of cards = "a"
     System.out.println(a);      // Prints deck after shuffling


    int cardsLeft = 51;

     card b;


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

      }


    }

1 个答案:

答案 0 :(得分:1)

在你的建筑设置卡片中留下52个。然后当你处理一张卡片时,将该数字减去一个

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

     }  
  }