2016-06-29 129 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的副本。这个问题现在与之相关,包含更广泛的解释。这个答案很好,我不需要在这里复制它:-) –