卡类java

时间:2013-12-05 17:36:11

标签: java arrays object arraylist

我正在制作卡片和套牌课程,制作一副52张卡牌,而cardProgram类是驱动程序。卡片类具有卡片套装的属性和值的属性(Ace,King,10,9等)。它还有一个属性的构造函数和一个卡的toString表示。甲板类是一个属性来表示卡片中的卡片,构造函数没有参数可以创建52张卡片并将它们添加到卡片组中,一种随机删除卡片并返回绘制的卡片对象的方法,以及一个toString显示卡片中包含的卡片。 Tha main将创建卡片组。用户输入他们想要绘制的数字。他们绘制的每张卡片都会打印出来,然后打印出卡片中剩余的卡片。我正在使用ArrayList执行此操作,我只想知道我是否朝着正确的方向前进。我一直在收到错误,我知道我的班级有问题。我之前从未使用过ArrayList,所以任何信息都会被赞赏

public class Card 
{
    private int type, value;
    private String[] cardType = {"Clubs", "Spades", "Diamonds", "Hearts"};
    private String[] cardValue = {"Ace", "King", "Queen", "Jack", "10",
                                   "9", "8", "7", "6", "5", "4", "3", "2"};

    public Card(int types, int values)
    {
        type = types; 
        value = values;
        //this.value = value;
    }

    public String toString()
    {
        String finalCard = cardValue[value] + " of " + cardType[type];

        return finalCard;
    }

}

import java.util.Random;
import java.util.ArrayList;

public class Deck 
{
    private ArrayList<Card> cards;

    public Deck()
    {
        for(int a =0; a<=3; a++)
        {
            for(int b =0; b<=12;b++)
            {
                cards.add(drawRandomCard());
            }
            cards.addAll(cards);
        }

    }

    public Card drawRandomCard()
    {
        Random generator = new Random();
        int index = generator.nextInt(cards.size());
        return cards.remove(index);
    }

    public String toString()
    {
        String result = "Cards remaining in deck: " + cards;

        return result;

    }    
}


import java.util.Scanner;

public class CardProgram 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Card C;
        Deck deck = new Deck();

        System.out.println("Enter number of cards to be dealt: ");
        int numberCards = scan.nextInt();

        System.out.println("Cards drawn: ");
        C = deck.drawRandomCard();

        System.out.println(C.toString());


    }

}

3 个答案:

答案 0 :(得分:0)

查看您的代码时,我认为您在谈论NullPointerException时会得到error

您已定义private ArrayList<Card> cards;,但您从未在Deck班级中对其进行初始化。

它应该像

private ArrayList<Card> cards; // currently its null
cards = new ArrayList<Card>(); // initializing here and no more null

答案 1 :(得分:0)

在设计指南方面,我会建议一些事情:

1)保持局部变量小写(Card c

2)考虑为rank和suit创建Java Enum。 你的套牌创建循环将看起来像这样:

cards = new ArrayList<Cards>();
for(Rank k : Rank.values())
   for(Suit s: Suit.values())
      cards.add(new Card(k,s));

3)你得到的错误可能是ArrayOutOfBoundsException,因为你的Deck构造函数在最初的牌为空时调用drawRandomCards()。祝你好运!

4)正如所指出的那样,你还没有初始化卡片。

答案 2 :(得分:0)

我认为这就是你所需要的。那我有什么改变?

1。 初始化套牌时,您需要为每个数字组合填充ArrayList一张卡片。这是通过cards.add(new Card(a,b));实现的。我还删除了cards.addAll(cards),因为您要将整个列表添加到自身。

编辑:哦,是的,我也初始化了ArrayList。那点有点重要!

2。 在CardProgram中,您获得了想要绘制的卡片数量,因此我多次拨打电话drawRandomCard()

3。 我还没有打印剩余的卡片,这取决于你。

public class Card 
{
private int type, value;
private String[] cardType = {"Clubs", "Spades", "Diamonds", "Hearts"};
private String[] cardValue = {"Ace", "King", "Queen", "Jack", "10",
                               "9", "8", "7", "6", "5", "4", "3", "2"};

public Card(int types, int values)
{
    type = types; 
    value = values;
    //this.value = value;
}

public String toString()
{
    String finalCard = cardValue[value] + " of " + cardType[type];

    return finalCard;
}

}

import java.util.Random;
import java.util.ArrayList;

public class Deck 
{
private ArrayList<Card> cards;

public Deck()
{
    cards = new ArrayList(52);
    for(int a =0; a<=3; a++)
    {
        for(int b =0; b<=12;b++)
        {
            cards.add(new Card(a,b));
        }
    }

}

public Card drawRandomCard()
{
    Random generator = new Random();
    int index = generator.nextInt(cards.size());
    return cards.remove(index);
}

public String toString()
{
    String result = "Cards remaining in deck: " + cards;

    return result;

}    
}


import java.util.Scanner;

public class CardProgram 
{
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
    Card C;
    Deck deck = new Deck();

    System.out.println("Enter number of cards to be dealt: ");
    int numberCards = scan.nextInt();

    System.out.println("Cards drawn: ");
    for (int i = 0; i < numberCards; i++) {
      C = deck.drawRandomCard();
      System.out.println(C.toString());
    }

    System.out.println(C.toString());


}

}