2016-06-29 168 views
0

如何改進我的代碼,使其在我打印方法時不會產生散列值?我認爲Card關鍵字後面的@ 2a ....等是散列值。Java - 輸出沒有正確打印

我的代碼產生下面的輸出,當我把我的方法來打印出來:

run: 
[email protected] 
[email protected] 
ca[email protected] 
[email protected] 

My code: 
public class Card { 

    /** 
    * @param args the command line arguments 
    */ 
    static String[] rank = {"2","3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; 
    static String[] suit = {"Spades","Hearts", "Clubs", "Diamonds"}; 

    public Card(String r, String s) 
    { 


    } 

    public static void init(Card[] deck) 
    { 
     for(int x = 0; x<deck.length; x++) 
     { 
      Card newCard = new Card(rank[x%13], suit[x/13]); 
      deck[x] = newCard; 
     } 
    } 

    public static void swap(Card[] deck, int a, int b) 
    { 
     Card temp = deck[a]; 
     deck[a] = deck[b]; 
     deck[b] = temp; 
    } 

    public static void shuffle(Card[] deck) 
    { 
     Random rnd = new Random(); 
     for(int x = 0; x<deck.length; x++) 
     { 
      swap(deck, x, (rnd.nextInt(deck.length))); 
     } 
    } 

    public static void print(Card[] deck) 
    { 
     for(int x = 0; x<deck.length; x++) 
      System.out.println(deck[x]); 
    } 


    public static void main(String[] args) { 
     // TODO code application logic here 
     Card[] deck = new Card[52]; 
     init(deck); 
     print(deck); 
    } 

回答

0

你應該重寫你的類卡的toString()方法。

public String toString() 
{ 
return "I want to print this"; 
} 
+0

你能詳細說一下嗎? – Aloysius

+1

此問題已正確標記爲@Tunaki的副本。這個問題現在與之相關,包含更廣泛的解釋。這個答案很好,我不需要在這裏複製它:-) –