调试二十一点程序? (Java)

Debugging Blackjack program? (Java)

我正在尝试编写一个 Blackjack 程序,用户可以在其中对 CPU 庄家下注。
我遇到的问题是我已经为卡片编写了 classes,牌组和经销商,但是当我尝试初始化新经销商和新牌组时,出现错误:

Exception in thread "main" java.lang.NullPointerException
at Blackjack.Deck.<init>(Deck.java:12)
at Blackjack.Blackjack.main(Blackjack.java:10)

这是我的class 卡片

 class Card {
      private int rank;
      private int suit;
      private int value;
      private static String[] ranks = {"Joker","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
      private static String[] suits = {"Clubs","Diamonds","Hearts","Spades"};

      Card(int suit, int values)
      {
           this.rank=values;
           this.suit=suit;
           if(rank>10)
           {
                value=10;
           }
           else
                value=rank;
      }
      public String toString()
      {
      return ranks[rank]+" of "+suits[suit];
      }
      public int getRank()
      {
      return rank;
      }
      public int getSuit()
      {
           return suit;
      }
      public int getValue()
      {
           return value;
      }
      public void setValue(int set)
      {
           value = set;
      }
 }

我的class用于甲板

package Blackjack;
import java.util.ArrayList;
import java.util.Random;

 class Deck {
      private ArrayList<Card> deck;
      Deck()
      {
           for(int i=0; i<4; i++)
           {
                for(int j=1; j<=13; j++)
                {
                     deck.add(new Card(i,j));
                }
           }
      }
      public void shuffle()
      {
           Random random = new Random();
           Card temp;
           for(int i=0; i<200; i++)
           {
                int index1 = random.nextInt(deck.size()-1);
                int index2 = random.nextInt(deck.size()-1);
                temp = deck.get(index2);
                deck.set(index2, deck.get(index1));
                deck.set(index1, temp);
           }
      }
      public Card drawCard()
      {
           return deck.remove(0);
      }
 }

我的 class 为 经销商:

package Blackjack;
import java.util.ArrayList;
import java.util.Arrays;
class Dealer {
ArrayList<Card> hand;
private int handvalue=0;
private Card[] aHand;
Dealer(Deck deck)
{
    hand = new ArrayList<>();
    aHand = new Card[]{};
    for(int i=0; i<2; i++)
    {
        hand.add(deck.drawCard());
    }
    aHand = hand.toArray(aHand);
    for(int i=0; i<aHand.length; i++)
    {
        handvalue += aHand[i].getValue();
        if(aHand[i].getValue()==1 && handvalue<12)
        {
            handvalue=handvalue+10;
        }
    }
}
public String showFirstCard()
{
    Card[] firstCard = new Card[]{};
    firstCard = hand.toArray(firstCard);
    return firstCard[0].toString();
}
public void Hit(Deck deck)
{
    hand.add(deck.drawCard());
    aHand = hand.toArray(aHand);
    handvalue = 0;
    for(int i=0; i<aHand.length; i++)
    {
        handvalue += aHand[i].getValue();
        if(aHand[i].getValue()==1 && handvalue<12)
        {
            handvalue=handvalue+10;
        }
    }
}
public boolean wantsToHit()
{
    if(handvalue<17)
    {
        return true;
    }
    return false;
}
public boolean hasBlackJack()
{
    if(hand.size()==2 && handvalue==21)
    {
        return true;
    }
    return false;
}
public String showHand()
{
    aHand = hand.toArray(aHand);
    String hands="";
    for(int i=0; i<aHand.length-1; i++)
    {
        hands = aHand[i].toString()+", ";
    }
    hands = hands + aHand[aHand.length-1].toString();
    return hands;
}
public int getHandValue()
{
    return handvalue;
}

}

还有我的主要方法:

 public class Blackjack {
 private static int cash;
 public static void main(String[] args){
      System.out.println("Hi! What is your name?");
      Scanner scan = new Scanner(System.in);
      String name = scan.nextLine();
      System.out.println("Hello, "+name+" lets plays some BlackJack!");
      Deck deck = new Deck();
      deck.shuffle();
      Dealer dealer = new Dealer(deck);
      System.out.println(dealer.showHand());
      System.out.println(dealer.getHandValue());
      }
 }

非常感谢任何帮助。谢谢!

Deck()里面,你需要添加这个:

deck = new ArrayList<Card>(); // Same for hand in Dealer class


在这里,

aHand = hand.toArray(aHand);
for(int i=0; i<aHand.length; i++)
{
    handvalue += aHand[i].getValue();
    if(aHand[i].getValue()==1 && handvalue<12)
    {
        handvalue=handvalue+10;
    }
}

可以改成

for(Card c : hand) {
    handvalue += c.getValue();
    // rest of code
    // And I think inside your if,
    // It should be handvalue += 9? You already added the value '1' before.
    // If I remember how to play blackjack correctly, counting Ace as 10?
}

Deck 中,属性 deck 从未被初始化,因此 null。由于 decknull 这一行:deck.add(new Card(i,j)); 将抛出 NullPointerException。只需在 for 循环之前创建 deck 即可解决此问题

Deck()
{
    deck = new ArrayList<>();

    for(int i=0; i<4; i++)
    {
        for(int j=1; j<=13; j++)
        {
            deck.add(new Card(i,j));
        }  
    }
}