如何为Java Card类创建一个remove方法

How do can create a remove method for the Java Card Class

卡类

公共类卡{

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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());

}

}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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());
    }
}


你无法改变array的大小,
我建议使用ArrayList

1
2
3
4
5
6
7
8
9
10
11
12
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);
}