这个方法的参数有什么问题/我为什么会收到这个错误?

时间:2015-04-21 20:17:17

标签: java methods arraylist parameters playing-cards

我已经包含了我在这个问题中写过的三个课程。一个是卡片类,代表扑克牌:

public class Card 
{
    int theRank, theSuit, theCard;

    public Card(int suit, int rank)
    { 
     theRank = rank;
     theSuit = suit;
    }

    public String getRank()
    {
     String rankString = "";

     switch (theRank)
     {
         case 1:
             rankString = "Ace";
         case 2: 
             rankString = "2";
         case 3:
             rankString = "3";
         case 4:
             rankString = "4";
         case 5:
             rankString = "5";
         case 6:
             rankString = "6";
         case 7:
             rankString = "7";
         case 8:
             rankString = "8";
         case 9:
             rankString = "9";
         case 10:
             rankString = "10";
         case 11:
             rankString = "Jack";
         case 12:
             rankString = "Queen";
         case 13:
             rankString = "King";
      }

      return rankString;
    }

    public String getSuit()
    {
     String suitString = "";

     switch(theSuit)
     {
         case 1:
             suitString = "Diamonds";
         case 2:
             suitString = "Hearts";
         case 3:
             suitString = "Clubs";
         case 4:
             suitString = "Spades";
     }

     return suitString;
  }
}

另一个是类Deck,它表示Card对象的arrayList:

public class Deck 
{

    public ArrayList<Card> loadDeck(ArrayList<Card> deck)
    {
     for (int suit = 1; suit <= 4; suit++)
     {
      for (int rank = 1; rank <= 13; rank++)
      {
       deck.add(new Card(suit, rank));
      }
     }

    return deck;
 }

 public void shuffleDeck(ArrayList<Card> deck)
 {
    Card temp;
    int index;
    Random rand = new Random();
    for (int i = deck.size() - 1; i > 0; i--)
    {
     index = rand.nextInt(i + 1);
     temp = deck.get(index);
     deck.add(index, deck.get(i));
     temp = deck.get(i);
    }

 }

public ArrayList dealHand(ArrayList<Card> deck)
{
    Random rand = new Random();
    ArrayList<Card> hand = new ArrayList<Card>(8);

    for (int i = 0; i <= 6; i++)
    {
     int suit = rand.nextInt(3);
     int rank = rand.nextInt(51);
     Card temp = new Card(suit, rank);
     hand.add(i, temp);
     deck.remove(temp);


    }

    return hand;
}

public void printHand(ArrayList<Card> hand)
{
    for (int i = 0; i <= 6; i++)
    {
     Card temp = hand.get(0);
     System.out.println(temp.getRank() + " of " + temp.getSuit());

    }
}

}

然后我有一个包含主要方法的GoFish类:

import java.util.ArrayList;

public class GoFish 
{

    public static void main(String[] args) 
    {
        ArrayList<Card> deck = new ArrayList<Card>();
        deck.loadDeck(deck);
    }
}

在main方法中,我创建一个名为“deck”的Card对象的arrayList。 loadDeck()方法应该将一个空的arrayList(用于Card对象)作为参数,并使用Card对象加载它,复制标准的52张扑克牌。我在main方法类中遇到了一个我不理解的错误,尽管我认为它与loadDeck()方法的参数有关。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

ArrayList没有方法loadDeck()

public static void main(String[] args) 
{
    ArrayList<Card> deck = new ArrayList<Card>();
    deck.loadDeck(deck);
}

你想:

public static void main(String[] args) 
{
    Deck testDeck = new Deck();
    ArrayList<Card> deck = new ArrayList<Card>();
    deck = testDeck.loadDeck(deck);
}

***************更新你可能想要这样的东西吗?************************

public class Deck 
{
    private ArrayList<Card> deck;

    public Deck()
    {
        deck = new ArrayList<Card>();
        loadDeck();
    }

    public void loadDeck()
    {
     for (int suit = 1; suit <= 4; suit++)
     {
      for (int rank = 1; rank <= 13; rank++)
      {
       deck.add(new Card(suit, rank));
      }
     }

    return deck;
 }

 public void shuffleDeck(ArrayList<Card> deck)
 {
    Card temp;
    int index;
    Random rand = new Random();
    for (int i = deck.size() - 1; i > 0; i--)
    {
     index = rand.nextInt(i + 1);
     temp = deck.get(index);
     deck.add(index, deck.get(i));
     temp = deck.get(i);
    }

 }

public ArrayList<Card> dealHand()
{
    Random rand = new Random();
    ArrayList<Card> hand = new ArrayList<Card>(8);

    for (int i = 0; i <= 6; i++)
    {
     int suit = rand.nextInt(3);
     int rank = rand.nextInt(51);
     Card temp = new Card(suit, rank);
     hand.add(i, temp);
     deck.remove(temp);


    }

    return hand;
}

public void printHand(ArrayList<Card> hand)
{
    for (int i = 0; i <= 6; i++)
    {
     Card temp = hand.get(0);
     System.out.println(temp.getRank() + " of " + temp.getSuit());

    }
}

public ArrayList<Card> getDeck()
{
    return this.deck;
}

}