Java二十一点游戏编译"变量未初始化错误"

时间:2018-05-22 13:39:57

标签: java

我正试图制作一种二十一点玩家。我有一个假设的方法,但我得到一个错误:

  

变量决策可能尚未初始化

我很困惑,我不太明白我做错了什么。这已经写好了,我宁愿不必切换到代码切换器。

以下是代码:

 public PlayerDecision decideHowToPlayHand(final GameInfo gameInfo, final PlayerHand currentHand,
      final List<PlayerHand> playerHands, final Card dealerUpCard) {

    PlayerDecision decision;

    System.out.println();
    System.out.println(
        "The dealer up card is a " + dealerUpCard + " with rank " + dealerUpCard.getRank());
    System.out.println("This hand has a score of (counting aces as 11 if possible) = "
        + currentHand.getPointScore());
    System.out
        .println("This hand has a score (counting aces as 1) = " + currentHand.getScoreAceAs1());
    System.out.println("You have the following cards in your hand:");
    for (final Card c : currentHand.getCards()) {
      System.out.println("\t" + c.toString() + " which has a rank of " + c.getRank());
    }

    // TODO: This is not the best solution, improve it!!!
if (dealerUpCard.getMinScore() == 2) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
      else decision = PlayerDecision.STAND;
    }
    else if (dealerUpCard.getMinScore() == 3) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
    else if (dealerUpCard.getMinScore() == 4) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
    else if (dealerUpCard.getMinScore() == 5) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
    else if (dealerUpCard.getMinScore() == 6) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
      else if (dealerUpCard.getMinScore() == 7) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
         else if (dealerUpCard.getMinScore() == 8) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
            else if (dealerUpCard.getMinScore() == 9) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
            else if (dealerUpCard.getMinScore() == 10) {
      if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
          else if (dealerUpCard.getMinScore() == 1) {
       if (currentHand.getPointScore() <= 8)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 9)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 10)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 11)
        decision = PlayerDecision.DOUBLE_DOWN;
      else if (currentHand.getPointScore() == 12)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 13)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 14)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 15)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 16)
        decision = PlayerDecision.HIT;
      else if (currentHand.getPointScore() == 17)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 18)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 19)
        decision = PlayerDecision.STAND;
      else if (currentHand.getPointScore() == 20)
        decision = PlayerDecision.STAND;
       else if (currentHand.getPointScore() == 21)
        decision = PlayerDecision.STAND;
    }
    return decision;
  }

2 个答案:

答案 0 :(得分:1)

这种结构

int value;
if (condition1) {
    value = 1;
} else if (condition2) {
    value = 2;
}
由于条件1和条件2可能永远不会发生,因此

不保证value可以被初始化。您确定只有两个条件,您需要将其更改为:

int value;
if (condition1) {
    value = 1;
} else {
    value = 2;
}

在这种情况下,如果dealerUpCard.getMinScore()不等于1-10中的任何一个,则decision将不会被初始化。您在嵌套的if条件中遇到类似的问题。要解决此问题,您可能需要为其指定默认值。或者您需要将最后else if更改为else

答案 1 :(得分:1)

Java希望确保最后的返回不会出现意外情况。在声明但没有初始化decision时,你没有足够明确让Java感到高兴。

假设调用此方法的代码可以处理它,您可以在声明它时将decision变量初始化为null

    PlayerDecision decision = null;

如果失败,您可以在PlayerDecision类中为UNKNOWNUNDEFINED添加额外状态,并在调用代码中处理它。

对于它的价值,在这种情况下,切换/选择语句通常更容易阅读: - )