尝试获取我的代码以在发卡后返回卡组中剩余的卡数。遇到一些问题...我无法解决它...这就是我所拥有的。我没有把经销商放进去,因为我还没有在这里工作。所以一旦我在这里开始工作,我就将其添加到我的经销商程序中。任何帮助表示赞赏...

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:


这是我的经销商.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);

      }


    }

最佳答案

在您的构造中,将牌数留为52。然后,当您发牌时,将该数字减一

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

     }
  }

09-20 16:59