我得到這個空指針異常,我不知道爲什麼java的撲克遊戲空指針
public static boolean hasPair(Card[] cards) {
int k=0;
cards = new Card[5];
for (int atPos = 0; atPos<5; atPos++){
for (int atPos2 = atPos+1; atPos2<5; atPos2++){
if(cards[atPos].getValue() == cards[atPos2].getValue()){
k++;
}
if (atPos2 == (cards.length-1) && k!=1){
k=0;
}
else if (atPos2 == (cards.length-1) && k>=2){
return true;
}
}
}
return false;
}
我的方法是測試我的手牌是否有持有相同值的兩張牌和nul指針說這是在這一行
if(cards[atPos].getValue() == cards[atPos2].getValue()){
我也有這種方法...我可以使用它作爲助手嗎?
public Card[] deal(int numCards) {
Card[] newArray;
newArray = new Card[numCards];
for (int index=0; index<numCards; index++){
newArray[index] = cards.get(0);
cards.remove(0);
}
return newArray;
}
既然你行'卡=新卡[5];'你的方法的頂部,你的'cards'陣列將永遠是空的。可能不是你想要的,因爲你正在向該方法傳遞一個數組。 – khelwood
我該如何解決這個問題? – masonft