2014-12-07 75 views
0

我一直在試圖洗牌,但輸出有錯誤。我想讓它顯示一張隨機牌,如Ace spade,兩心三王等。但是,結果如下所示。我如何更改我的代碼?如何在Java中洗牌甲板?

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

代碼:

import java.util.Random; 

public class deck3 { 

    //public int TOTALCARD; 
    public Card[] deck; 

    public deck3() { 
     this.deck = new Card[52]; 
     String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"};// Array for 
     // suit 
     int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Array 

     for (int i = 0; i < suit.length; i++) { 
      for (int n = 0; n < number.length; n++) { 
       Card c = new Card(suit[i], number[n]); // Card instantiate 

       // Using (i*number.length + n) will get you from index 0 to 51 
       if (deck[(i * number.length + n)] == null) { 
        deck[(i * number.length + n)] = c; // Accessing index 0 to 
        // 51 
       } 
      } 
     } 
    } 

    public void CreateDeck() {} 

    //int[] TOTALCARD={deck}; 
    public void shuffleDeck() { 

     Random generator = new Random(); 
     for (int i = 0; i < deck.length; i++) { 
      int thisplace = generator.nextInt(50); 
      int thatplace = generator.nextInt(50); 

      Card savednumber = deck[thisplace]; 
      deck[thisplace] = deck[thatplace]; 
      deck[thatplace] = savednumber; 
      System.out.println(savednumber); 
     } 

    } 

    public void displayDeck() { 
     for (int i = 0; i < deck.length; i++) { 
      this.deck[i].display(); 
      //shuffleDeck();// Calling the display method from my card 
      // class. 
      //System.out.println(randomNumbers); 
     } 
    } 
} 
+1

您也可以使用'Collections.shuffle' – cybersoft 2014-12-07 10:08:10

+0

打印此輸出的方法是什麼?另請發佈您的'Card.display()'實現。 – Elist 2014-12-07 10:21:08

+0

添加一個公共字符串toString(),它打印套裝和數字。 – 2014-12-07 10:35:41

回答

0

重寫toString方法在Card類。
然後使用相同的代碼打印出Card對象。
這應該做到這一點。

Object.toString

3

你需要覆蓋公共字符串的toString()類卡的漂亮的打印方法(而不是卡@ b5f53a,...)。 例如:

class Card { 
    private String value; 
    private Long value2; 

    @Override 
    public String toString() { 
     return "Card{" + 
       "value='" + value + '\'' + 
       ", value2=" + value2 + 
       '}'; 
     } 
    } 

如果你需要洗牌 - 更好的方式使用它Collections.shuffle()。 例如:

Card[] cards = new Card[10]; 
    .... // you cards array (may be you can use List?) 
    List<Card> cardList = new ArrayList<Card>(); 
    Collections.addAll(cardList, cards); 
    Collections.shuffle(cardList); 
    //transform to array (if you need array) 
    cards = cardList.toArray(new Card[cardList.size()]); 

正如你看到的,你可以改用列表陣列

0

的有一個在你的函數deck.display()你打印的是你的卡類ID,而不是他們的數據字段的錯誤。 (我建議你正在製作類似System.out.print(card[i])這是錯誤的,因爲程序不知道你想從該類打印什麼值,所以它打印類ID)

你可能想要實現一個功能display()card類,如下所示:

public void display() 
{ 
    System.out.println("Card Number = "+number+", Card Suit = "+suit); 
} 

然後當你調用deck.display()您可以致電card[i].display()爲所有卡片。 另外考慮使用卡的西裝的int值,這是更輕量級。