-1

我正在制作与卡相关的应用程序。

花色分为梅花、方块、黑桃、红心

我想这样订购它们:

在牌中,Ace 是最高的,2 是最低的,所以标准的 Ace King Queen Jack 顺序就可以了。

我正在尝试使用 compareTo 方法。我完全不明白。

我是一个视觉学习者,所以代码示例,例如如何使用该方法的逐步演练会 - 真的 - 帮助我。它不必与我的代码相关,任何我可以通过视觉学习来学习和尝试实现的东西都会在这一点上有所帮助。

到目前为止,这是我的代码。如果有人可以告诉我 - 我应该在哪里实现这个,那也会有所帮助。

import java.util.Arrays;

public class PlayingCard implements Comparable {

// Class Constants
public static final int ACE = 1;
public static final int KING = 13;
public static final int QUEEN = 12;
public static final int JACK = 11;
public static final String SPADES = "spades";
public static final String CLUBS = "clubs";
public static final String HEARTS = "hearts";
public static final String DIAMONDS = "diamonds";

// Instance Variables
private int rank;
private String suit;

// Constructor
public PlayingCard () {
this.rank = PlayingCard.QUEEN;
this.suit = PlayingCard.SPADES;
}

public PlayingCard (int rank, String suit) {
this.rank = rank;
this.suit = suit;
Arrays.sort();
}

// Mutators
public void setRank (int rank) {
this.rank = rank;
}
public void setSuit (String suit) {
this.suit = suit;
}

// Accessors
public int getRank () {
return this.rank;
}
public String getSuit () {
return this.suit;
}

public String toString () {
return PlayingCard.rankToString(this.rank) + " of " + this.suit;
}

//Class Method
public static String rankToString(int rank) {
switch (rank) {
case(1): return "Ace";
case(2): return "two";
case(3): return "three";
case(4): return "four";
case(5): return "five";
case(6): return "six";
case(7): return "seven";
case(8): return "eight";
case(9): return "nine";
case(10): return "ten";
case(11): return "Jack";
case(12): return "Queen";
case(13): return "King";
}
return "INVALID";
}

public static void main(String [] args) {
// Generate an array of playing cards
PlayingCard [] deck = new PlayingCard[52];

String [] suits = {PlayingCard.CLUBS, PlayingCard.DIAMONDS, PlayingCard.SPADES, PlayingCard.HEARTS};

for(int i = 0; i < suits.length; i++) { // Run through each suit
for (int j = 0; j < 13; j++) { // Make each card
deck[i*13 + j] = new PlayingCard(j+1, suits[i]);
}
}

// Shuffle cards
for(int i = 0; i < deck.length; i++) {
int newPos = (int)(Math.random()*52);
PlayingCard temp = deck[i];
deck[i] = deck[newPos];
deck[newPos] = temp;
}

// Print out cards
System.out.println("Shuffled Deck");
for(int i = 0; i < deck.length; i++) {
System.out.println(deck[i]);
}

// Sort the deck
Arrays.sort(deck);

// Print out cards
System.out.println("\n\nSorted Deck");
for(int i = 0; i < deck.length; i++) {
System.out.println(deck[i]);
}
}
}
4

2 回答 2

1

compareTo这是该课程的示例Integer

public int compareTo(Object o) {
    return this.value - ((Integer)o).value;
}

这不是实际的实现,因为如果您正在处理一个小的负数和一个大的正数,结果可能会溢出。但是,这种方法在许多情况下就足够了,包括 OP。

这会减去int这个对象的int值和另一个对象的值。想一想为什么会这样:

  • 如果两个数字相等,则减法得出 0,因此数字被理解为相等。
  • 如果this大于o,则减法产生正值,因此this被理解为更大。
  • 如果this小于o,则减法产生负值,因此o被理解为更大。

为了帮助使用这种数字方法,您可能应该给出以适当顺序排列的花色整数值而不是字符串值,并定义ACE为 be14而不是1,因为 ace 大于国王,即13。有了这个,您可以编写一个结合两种比较策略的方法:

public int compareTo(Object o) {
    PlayingCard other = (PlayingCard) o;
    int result = this.suit - other.suit;
    if (result != 0) 
        return result;
    return this.rank - other.rank;
}

这将首先比较花色,如果它们相同,它将比较等级。

你必须把compareTo方法放在PlayingCard类中,否则如果你实现它就不会编译Comparable

于 2013-08-04T09:31:09.623 回答
1

花色和等级应该用枚举数来实现。如果您使用两个类,也更容易阅读代码;甲板类和卡片类。

我用枚举器和两个类重新制作了程序:

public enum Suit {
    Hearts("H"), Spades("S"), Clubs("C"), Diamonds("D");

    private final String shortName;

    private Suit(String shortName) {
        this.shortName = shortName;
    }

    public String getShortName() {
        return shortName;
    }
}

public enum Rank {
    King("K"), Queen("Q"), Jack("J"), Ten("10"), Nine("9"), Eight("8"), Seven("7"), Six("6"), Five("5"), Four("4"), Three("3"), Two("2"), Ace("E");

    private final String shortName;

    private Rank(String shortName) {
        this.shortName = shortName;
    }

    public String getShortName() {
        return shortName;
    }
}

public class PlayingCard implements Comparable<PlayingCard> {
    private Suit suit;
    private Rank rank;

    public PlayingCard(Suit suit, Rank rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getShortName() {
        return suit.getShortName() + rank.getShortName();
    }

    public String toString() {
        return rank + " of " + suit;
    }

    public Suit getSuit() {
        return suit;
    }

    public Rank getRank() {
        return rank;
    }

    @Override   
    public int compareTo(PlayingCard other) {
        // First compares the ranks and if they're equal the cards are sorted
        // by their suits. The rank and suit are always sorted in the order
        // they're declared in the enumerators.

        if(rank.compareTo(other.getRank()) == 0) {
            return suit.compareTo(other.getSuit());
        } else {
            return rank.compareTo(other.getRank());
        }
    }
}

public class Deck {
    private ArrayList<PlayingCard> deck = new ArrayList<PlayingCard>();

    public Deck() {
        for(Suit s : Suit.values()) {
            for(Rank r : Rank.values()) {
                deck.add(new PlayingCard(s, r));
            }
        }
    }

    public void shuffle() {
        Collections.shuffle(deck);
    }

    public void sort() {
        Collections.sort(deck);
    }

    public void addToBottom(PlayingCard c) {
        deck.add(c);
    }

    public PlayingCard removeTopCard() {
        return deck.remove(0);
    }

    public PlayingCard peekTop() {
        return deck.get(0);
    }

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

    public String toString() {
        StringBuilder sb = new StringBuilder();
            for(PlayingCard c : deck) {
                sb.append(c.getShortName() + ' ');
            }
        return sb.toString();
    }


    public static void main(String[] args) {
        Deck deck = new Deck();
        System.out.println(deck);

        deck.shuffle();
        System.out.println(deck);

        deck.sort();
        System.out.println(deck);

        System.out.println(deck.peekTop());
    }
}

该代码创建了一个牌组,将其洗牌并排序。它在每次操作后打印卡片组。最后它打印牌组的顶牌。main() 的一个示例输出是:

HK HQ HJ H10 H9 H8 H7 H6 H5 H4 H3 H2 HE SK SQ SJ S10 S9 S8 S7 S6 S5 S4 S3 S2 SE CK CQ CJ C10 C9 C8 C7 C6 C5 C4 C3 C2 CE DK DQ DJ D10 D9 D8 D7 D6 D5 D4 D3 D2 DE 
D7 CE C8 C5 C9 DE SK H7 DQ DK D3 C7 C4 D2 D5 C6 S3 H10 S10 D10 S4 SJ D6 CQ CK D4 H8 H9 S6 HJ HE S8 S7 DJ C10 S2 SQ CJ HK C2 H2 C3 H5 H3 HQ D9 H6 S9 S5 SE H4 D8 
HK SK CK DK HQ SQ CQ DQ HJ SJ CJ DJ H10 S10 C10 D10 H9 S9 C9 D9 H8 S8 C8 D8 H7 S7 C7 D7 H6 S6 C6 D6 H5 S5 C5 D5 H4 S4 C4 D4 H3 S3 C3 D3 H2 S2 C2 D2 HE SE CE DE 
King of Hearts
于 2013-08-04T11:10:09.470 回答