2014-12-07 64 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值,这是更轻量级。