2013-08-04 129 views
-1

我正在製作一個卡相關的應用程序。在這段Java代碼中我需要比較哪些變量?

西裝都排梅花,方塊,黑桃,紅桃

我想訂他們是這樣的:

內卡,ACE是最高的,2是最低的,因此標準王牌女王傑克訂單將罰款。

我正在嘗試使用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]); 
} 
} 
} 
+1

-1。谷歌搜索「實施compareTo」提供了很多關於這方面的信息,包括例子。 – GenericJon

回答

1

下面是一個例子爲compareToInteger類:

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

這是不實際執行,因爲如果你正在處理一個小的負數和一個大的正數,結果可能會溢出。但是,在許多情況下,這種方法已經足夠,包括OP的。

這會減去此對象的int值和另一個對象的int值。想一想爲什麼這樣做:

  • 如果兩個數字相等,減法得出0,所以數字被理解爲相等。
  • 如果this大於o,則減法得出正值,所以this被理解爲更大。
  • 如果this小於o,則減法產生負值,因此o被理解爲更大。

爲了解決這個數值方法,你或許應該給予適當的順序,而不是字符串值排名西服整數值,並定義ACE14,而不是1,因爲王牌是比國王更大,這是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,它將不會編譯。

+0

完美。你從字面上把谷歌給我的信息分散在一起。謝謝。 –

+0

我甚至沒有谷歌它。所有這些都讓我頭腦發熱。 – tbodt

+2

您提出了'Integer#compareTo'的錯誤實現。由於溢出而導致兩個大的負數失敗。真正的實現是這樣的:'return(x

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 
相關問題