如何为Java Card类

时间:2017-11-10 04:56:31

标签: java

卡片类

公共类卡{

private int value;
private String rank;
private String suit;

public Card(String s, String r, int v) { suit=s;rank=r;value=v; }

public int getValue() {return value;}
public String getRank() {return rank;}
public String getSuit() {return suit;}

public boolean matches(Card othercard) {
    return othercard.getValue()==(this.value)
            &&othercard.getRank().equals(this.rank)
            &&othercard.getSuit().equals(this.suit);
}

@Override
public String toString() {
    return "[" + suit +", "+ rank + ", " + value + "]";
}

}

使用remove方法的deck类。 我修改了显示方法,不显示空卡,并使大小返回所有卡,排除空卡。删除也是固定的。感谢

import java.util.ArrayList;

公共课Deck01 {

private Card[] cards;
private int size;

public Deck01(){
    cards = new Card[52];
    int size = 0;
}
public void add(String suit, String rank, int value){
    Card temp = new Card(suit,rank,value);
    cards[size] = temp;
    size++;
}

public void remove(String suit, String rank, int value){
    Card temp = new Card(suit,rank,value);
    for (int s = 0;s<size;s++) {
        if ((temp.getValue())  == (-1)
                &&temp.getRank()==(null)
                &&temp.getSuit()==(null)) {
            s++;
        }
        if (temp.getValue()==(cards[s].getValue())
                &&temp.getRank().equals(cards[s].getRank())
                &&temp.getSuit().equals(cards[s].getSuit())) {
            cards[s]= new Card(null,null,-1);

        }           
    }

}

public void display() {
    int q=-1;
    for (int k = 0;k<size;k++) {
        if (cards[k].getValue() != (q))
            System.out.println(cards[k]);
    }
}

public int getSize(){
    int nullsize=0;
    for (int i = 0;i<size;i++) {
        if ((cards[i].getSuit())==(null))
            nullsize++;
    }

    return size-nullsize;
    }

public boolean isEmpty(){return size==0;}

@Override
public String toString() {
    return "Deck01 [cards=" + cards + ", size=" + size + "]";
}

}

主要班级......

import java.util。*;

公共类DeckTester01 {

public static void main(String[] args) {
    Deck01 deck =new Deck01();

    System.out.println(deck.getSize());
    System.out.println(deck.isEmpty());
    deck.add("Clovers","Jack",11);
    deck.add("Diamonds","King",13);
    deck.add("Hearts","Queen",12);
    deck.add("Spades","9",9);
    deck.add("Clovers","7",7);
    deck.add("Diamonds","Ace",1);
    deck.add("Hearts","Jack",11);
    deck.add("Spades","Ace",1);
    deck.add("Clovers","3",3);
    deck.add("Diamonds","5",5);     


    deck.display();
    System.out.println(deck.isEmpty());
    System.out.println(deck.getSize());

    deck.remove("Hearts","Jack",11);
    deck.remove("Diamond","King",13);
    deck.remove("Spades","Ace",1);
    deck.display();
    System.out.println(deck.getSize());

}

}

2 个答案:

答案 0 :(得分:0)

您无法更改array的大小,
我建议使用ArrayList

private List<Card> deck = new ArrayList();

//you don't need class Deck anymore

deck.add(new Card("Clovers","Jack",11)); // to add
deck.remove(new Card("Clovers","Jack",11)); // to remove
System.out.println(deck.size()); // to print n. of cards

// to display
for (Card c: deck){
    System.out.println(c);
}

答案 1 :(得分:0)

您可以尝试使用LinkedList来实现此目的。这是我的代码有效。你在删除功能中犯了错误。您应该按索引删除不是大小。那么你甚至不需要大小变量。

import java.util.LinkedList;
import java.util.List;

public class Deck01 {
    private List<Card> cards;

    public Deck01(){
        cards = new LinkedList<>();
    }
    public void add(String suit, String rank, int value){
        Card temp = new Card(suit,rank,value);
        cards.add(temp);
    }

    public void remove(String suit, String rank, int value){
        Card temp = new Card(suit,rank,value);
        for (int s = 0;s<cards.size();s++) {
            Card tempCard = cards.get(s);
            if (temp.getValue()==(tempCard.getValue())
                    &&temp.getRank().equals(tempCard.getRank())
                    &&temp.getSuit().equals(tempCard.getSuit())) {
                cards.remove(tempCard);
                break;
            }
        }
    }

    public void display() {
        System.out.println(cards);
    }

    public int getSize(){return cards.size();}

    public boolean isEmpty(){ return cards.isEmpty(); }

    @Override
    public String toString() {
        return "Deck01 [cards=" + cards + ", size=" + cards.size() + "]";
    }

    public static void main(String[] args) {
        Deck01 deck =new Deck01();

        System.out.println(deck.getSize());
        System.out.println(deck.isEmpty());
        deck.add("Clovers","Jack",11);
        deck.add("Diamonds","King",13);
        deck.add("Hearts","Queen",12);
        deck.add("Spades","9",9);
        deck.add("Clovers","7",7);
        deck.add("Diamonds","Ace",1);
        deck.add("Hearts","Jack",11);
        deck.add("Spades","Ace",1);
        deck.add("Clovers","3",3);
        deck.add("Diamonds","5",5);

        deck.display();
        System.out.println(deck.isEmpty());
        System.out.println(deck.getSize());

        deck.remove("Diamonds","5",5);
        deck.remove("Spades","Ace",1);
        deck.display();
        System.out.println(deck.getSize());
    }
}