【发布时间】:2015-01-18 03:29:20
【问题描述】:
基本上我尝试在测试程序中运行deckTest() 以打印出洗牌后的牌组(删除deckTest 上的第一个和最后一个评论)。我得到的只是 Deck 类的 toString 方法中 answer=answer+ "\n" + deck[i].toString(); 行上的 nullpointerEx。
但是当我尝试打印出 Pre-shuffled 牌组(在 testDeck() 中打开 cmets)时,它很好。
deckTest() 中的第二条注释实际上显示了洗好的牌组的第一张牌,我认为洗牌至少有效!
感谢你们所有人,祝您 2015 年愉快! 威廉斯
有测试程序:
public class TestCard
{
public static void main ()
{
Card c1=new Card(0,1);
Card c2=new Card(3,13);
System.out.println(c1.toString());
System.out.println(c2.toString());
}
public static void deckTest()
{
Deck d1= new Deck();
System.out.println(d1);
//d1.shuffle();
//System.out.println(d1.dealCard().toString());
//System.out.println(d1.toString());
}
}
有它所指的甲板类:
import java.util.Random;// for shuffing the cards
public class Deck
{
private final int NUMOFCARDS= 52;
private int numCards;
private Card[] deck= new Card[53];
/**
* Constructor for objects of class deck
*/
public Deck()
{
int c = 0;
for (int s=0;s<=3;s++)
{
for (int n=1;n<=13;n++){
deck[c]=new Card(s,n);
c++;
}
}
numCards=NUMOFCARDS;
}
/**
* Use this method to display all of the cards in the deck
*/
public String toString()
{
String answer = "";
for (int i=0;i<53;i++)
{
answer= answer + "\n" + deck[i].toString();
}
return answer;
}
/**
* returns true of the current number of cards in the deck equals to 0
*/
public boolean empty()
{
return numCards==0;
}
/**
* pull the bottem card from the deck
* the variable
*/
public Card dealCard()
{
if (empty())
{
System.out.println("the deck has run out of cards, there will be a new,preshufffled deck to continue");
//shuffle(); //shuffle cards
numCards=NUMOFCARDS; //reset dealPosition for dealing new deck
}
numCards--;
return deck[52-numCards];
}
public void shuffle()
{
Random random = new Random(); // creat a random object
Card memory;
int randomPosition ;
for (int i=0;i<53;i++)
{
randomPosition = random.nextInt(53); // assign a number between 0 to 52 as randomPosition for shuffle
memory=deck[i]; // store the current deck[i] card
deck[i]=deck[randomPosition]; //assign new card to current card
deck[randomPosition]=memory; //assign current card to new card
}
}
}
有卡片组和测试方法所指的 Card 类:
public class Card
{
final int JACK = 11;
final int QUEEN = 12;
final int KING = 13;
final int ACE = 1;
private int num;
private int suit;
final int SPADES = 0;
final int HEARTS = 1;
final int DIAMONDS = 2;
final int CLUBS = 3;
public Card(int theSuit,int theNum)
{
num=theNum;
suit=theSuit;
}
public String showSuit()
{
if (suit==0)
{
return "Spades";
}
if (suit==1)
{
return "Hearts";
}
if (suit==2)
{
return "Diamonds";
}
if (suit==3)
{
return "Clubs";
}
return "";
}
public String showNum()
{
if (num==11)
{
return "Jack";
}
if (num==12)
{
return "Queen";
}
if (num==13)
{
return "King";
}
if (num==1)
{
return "Ace";
}
return ""+num;
}
public String toString()
{
return " "+ showNum() + " of " + showSuit();
}
public boolean equals(Card theCard)
{
return theCard.toString().equals(toString());
}
【问题讨论】:
-
为什么
Deck.deck53 个元素长? -
@immibis 也许删除了百搭牌,但没有删除“抽牌和梭哈扑克规则”?
-
我问是因为,因为你只初始化了 52 个元素,其中一个是空的。而且你的 toString 不处理 null。
-
感谢您的帮助!我很感激你的cmets
标签: java arrays nullpointerexception tostring blackjack