CardPile错误,无法编译它。

时间:2015-03-23 04:50:55

标签: java javascript

我正在尝试编译这段代码,它给了我一个错误,我不知道如何解决它。我正在尝试为Go Fish游戏编译CardPile类。

 import java.util.Random;


    public class CardPile {
  enter code hereprivate Card[] cards;
  private int numCards;
  private static Random n = new Random(1);


  public CardPile() {
    this.numCards = 0;
    this.cards = new Card[52];
  }

  public void addToBottom(Card c) {
    this.cards[numCards] = c;
    numCards++;
  }
  public Card removeCard(int ind) {
    Card toRemove = this.cards[ind];
    for (int i = ind; i < numCards - 1; i++) {
      this.cards[i] = this.cards[i+1];
    }
    this.cards[numCards-1] = null;
    numCards--;
    return toRemove;
  }

  public Card removetop() {
    Card topCard = cards[0];
    Card[] cards = new Card[cards.length - 1];
    int counter = 0;
    for(int i = 1; i < cards.length; i++) {
      cards[counter] = cards[i];
      counter++;
    }
    this.cards = cards;
    return topCard;
  }

  public int searchValue(int value) {
    int count = 0;
    for (int i = 0;i < cards.length;i++) {
      if (cards(i) == value) {       GIVES ME A CANNOT FIND SYMBOL HERE
        count++;
      }
    }
    return count;
  }

  public Card[] removeAll(int value) {
    int count = searchValue(value);
    Card[] removed = new Card[count];
    for (int i = 0; i < cards.length;i++) {
     [if (cards(i) != value) {         GIVES ME A CANNOT FIND SYMBOL HERE
      }
    }
    this.cards.toArray(removed);       AND HERE AS WELL.
    return removed;
  }

  public int getNumberCards() {
    return this.numCards;
  }


  public String toString() {
    String word = "";
    for (int i = 0; i < numCards; i++) {
      word = word + "[" +  i + "]";
      word = word + this.cards[i];
      word = word + " ";
    }
    return word;
  }


  public void shuffle() {
    for (int i = 0; i < 100000; i++) {
      int first = n.nextInt(numCards);
      int second = n.nextInt(numCards);
      Card temp = this.cards[first];
      this.cards[first] = this.cards[second];
      this.cards[second] = temp;
    }
  }

  public static CardPile makeFullDeck() {
    CardPile deck = new CardPile();
    for (int suit = 0;suit < 4;suit++) {
      for (int value = 1; value < 14;value++) {
        deck.addToBottom(new Card(suit, value));
      }
    }
    deck.shuffle();
    return deck;
  }
}

我指定了我遇到问题的地方。

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

使用括号代替..

cards(i)

cards[i]

答案 1 :(得分:0)

当您访问数组时,使用方括号cards[i],而不是括号cards(i)